Python 关于if elif语句格式的快速问题
如果我想要一组If-elif语句按单个顺序执行;例如:Python 关于if elif语句格式的快速问题,python,if-statement,Python,If Statement,如果我想要一组If-elif语句按单个顺序执行;例如: if int(one[1]) == solution: 及 要执行单个语句,请执行以下操作: print "Hello World" 最好的格式和语法正确的输入方式是什么 我试过: if int(two[1]) == solution:: elif int(one[1]) == solution: print "Hello World" 和其他变化,但不能把我的数字在一个正确的方式来做。别客气,我编程时间不长
if int(one[1]) == solution:
及
要执行单个语句,请执行以下操作:
print "Hello World"
最好的格式和语法正确的输入方式是什么
我试过:
if int(two[1]) == solution::
elif int(one[1]) == solution:
print "Hello World"
和其他变化,但不能把我的数字在一个正确的方式来做。别客气,我编程时间不长
我用的语言是Python。
谢谢
更新:
我认为这是正确的答案,而不是阿卜杜勒·卡德尔。谢谢你们两位。在Python中,我想我将键入:
if (int(one[1]) == solution OR int(two[1])== solution)):
print "Hello World"
如果要在两个条件均为真时打印,则必须使用<代码>和
if (int(one[1]) == solution) and (int(two[1]) == solution):
print "Hello, world"
如果要在一个或多个条件为真时打印,则必须以相同的方式使用或
if (int(one[1]) == solution) or (int(two[1]) == solution):
print "Hello, world"
您不应该使用elif
。至少如果我理解你的话
<代码> ELIF < /代码>用于测试不同的条件,如果<代码>如果语句为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 false
附言:
如果是垃圾邮件或火腿,这确实是一种扭曲的做法,但它确实很丑陋你敢用这个吗,否则你会被炸成碎片你所需要的只是一个or:
if (int(one[1]) == solution) and (int(two[1]) == solution):
print 'Helloworld'
if (int(one[1]) == solution) or (int(two[1]) == solution)):
print "Hello World"
您还可以使用列表而不是“或”,这在有很多选择时非常方便<代码>如果[int(一[1])、int(二[1])、int(三[0])]中的解决方案:编辑使其系统错误。不能将“elif”放在“if”块中。
if solution in [int(one[1]), int(two[1]), int(three[0])]:
if (int(one[1]) == solution) or (int(two[1]) == solution)):
print "Hello World"
if int(one[1]) == solution or int(two[1]) == solution:
print "Hello world"
if solution in [int(one[1]), int(two[1]), int(three[0])]: