我想写一个小东西,给a
一个值,直到它与b
匹配,然后你就赢了。所以我对a>b
和a<b
使用了if
状态元。if a+1==b
的"you are near"被打印出来,显然a<b
的corrispondant语句也被打印出来了,但我不希望这样。如果a+1==b
和a<b
都为真,我如何只命令打印"你在附近"而不打印其他语句?
我现在的位置:
b = 5
a = None
c = 1
while a != b:
a = int(input('choose'))
if a +c ==b:
print ('almost there')
if a < b:
print('low')
if a > b:
print ('high')
b = 5
a = None
c = 1
while a != b:
a = int(input('choose'))
if a +c ==b:
print ('almost there')
elif a < b:
print('low')
elif a > b:
print ('high')
执行在if语句处分支,并在elif
或else
中继续新if意味着过去分支的结束和新分支的开始。
如果只想运行条件的一个分支,请使用elif
。
b = 5
a = None
c = 1
while a != b:
a = int(input('choose'))
if a +c ==b:
print ('almost there')
elif a < b:
print('low')
elif a > b:
print ('high')