如果其他语句为true,则不打印true语句



我想写一个小东西,给a一个值,直到它与b匹配,然后你就赢了。所以我对a>ba<b使用了if状态元。if a+1==b的"you are near"被打印出来,显然a<b的corrispondant语句也被打印出来了,但我不希望这样。如果a+1==ba<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语句处分支,并在elifelse中继续新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')

最新更新