本文介绍了C"< test>中的复合语句?< true-val>:< false-val>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨 我一直无法弄清楚如何从C进行复合声明 - "< test>?< true -val>:< false-val>" 但类似的东西必须存在......?! 我想要做等效的C行的python: printf(我看到%d car%s \ n,n,n!= 1?" s":"" ) 请帮助 / Holger 解决方案 在这种特殊情况下,你不需要三元运算符: print"我看到%d car%s \ n" %(n,("," s")[n!= 1]) 干杯, Jussi 最后一个换行符可能是不必要的。这似乎有点多了 可读: print" I saw",n," car" +(""," s")[n!= 1] 使用Python 2.5,这看起来更好: print" I seen" ;,n,汽车; +("" if n == 1 else" s") 或者我更喜欢的版本: print" I seen" ,n,(汽车,如果n == 1其他汽车) 那些()不是必需的,但它们有助于提高可读性,并且 也避免了运算符优先级问题。如果有一个相当低的 优先。 再见, 。熊市场 HiI have not been able to figure out how to do compound statement from C- "<test>?<true-val>:<false-val>"But something similar must exist...?!I would like to do the equivalent if python of the C line:printf("I saw %d car%s\n", n, n != 1 ? "s" : "")Please help/Holger 解决方案In this particular case you don''t need the ternary operator:print "I saw %d car%s\n" % (n, ("", "s")[n != 1])Cheers,JussiThe last newline is probably unnecessary. This seems be a bit morereadable:print "I saw", n, "car" + ("", "s")[n != 1]With Python 2.5 this looks better:print "I saw", n, "car" + ("" if n == 1 else "s")Or the vesion I like better:print "I saw", n, ("car" if n == 1 else "cars")Those () aren''t necessary, but they help improve readability, andavoid problems with operator precedence too. That if has a quite lowprecedence.Bye,bearophile 这篇关于C"< test>中的复合语句?< true-val>:< false-val>"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 02:37