如果我想要一组If elif语句以单一顺序执行;例如:
if int(one[1]) == solution:和
if int(two[1]) == solution:要执行单个语句,请执行以下操作:
print "Hello World"输入它的最佳格式和语法正确的方式是什么?
我试过了:
if int(two[1]) == solution::
elif int(one[1]) == solution:
print "Hello World"和其他变体,但不能把我的数字放在正确的方式上。温柔点,StackOverflow,我编程时间不长。
我使用的编程语言是Python。谢谢!
更新:
我认为这是正确的答案,而不是阿卜杜勒·卡德尔。不过还是要谢谢你们。在Python中,我想我会输入:
if (int(one[1]) == solution OR int(two[1])== solution)):
print "Hello World"发布于 2011-05-24 20:48:02
如果想要在两个条件都为真时打印它,则必须使用and
if (int(one[1]) == solution) and (int(two[1]) == solution):
print "Hello, world"如果您想打印它,如果一个或多个条件为真,则必须以相同的方式使用or
if (int(one[1]) == solution) or (int(two[1]) == solution):
print "Hello, world" 您不应该使用elif。至少如果我理解你的话。
elif用于测试不同的条件如果if语句中的条件为false,请考虑以下模式
if spam:
do_eggs() # We arrive here if spam is true
elif ham:
do_sausages() # Here if spam was false but ham is true
else:
print "Camelot!" # And here if both were falsePS:
if spam:
do_eggs()
elif ham:
do_eggs()这确实是做if spam or ham: do_eggs()的一种扭曲的方式,但它真的很丑陋。你不敢用这个,否则你会被炸成碎片。
发布于 2011-05-24 20:49:35
if int(one[1]) == solution or int(two[1]) == solution:
print "Hello world"发布于 2011-05-24 20:48:17
if (int(one[1]) == solution) and (int(two[1]) == solution):
print 'Helloworld'https://stackoverflow.com/questions/6110601
复制相似问题