我有多个if语句的问题。我明白我的代码有什么问题,但我找不到解决方案。所以当我运行该程序时,“较高”部分可以工作,但如果我猜“较低”部分则不能。解释者从阅读if语句到elif语句。那不是我想要的。如何在使用elif语句前先检查两个语句。我试过嵌套如果但我似乎无法得到它的工作。 在此先感谢。使更高或更低的游戏运行Python 3的问题。多个if语句
# Higher or lower card game
import random
x = random.randint(1, 14)
y = random.randint(1, 14)
print('The number is ', x, '.')
while True:
print('higher or lower?')
if input() in {'higher', 'h'} and y >= x:
print('Good guess the number was ', y)
x = y
y = random.randint(1, 14)
elif y < x:
print('Bad guess , the number was ', y)
break
if input() in {'lower', 'l'} and y < x:
print('Good guess, the number was ', y)
x = y
y = random.randint(1, 14)
elif y >= x:
print('Bad guess, the number was ', y)
break
continue
你想每次迭代只输入一次吗? –
我是新来的平台,似乎我搞砸了缩进。我修好了 – EnCoder
Coldspeed,我想让它在打印x的值后输入一个数字。如果可能,我希望if语句独立工作。所以如果我选择回答'lower',它应该读取第二个代码块并跳过第一个。但是elif声明造成了一个问题。 – EnCoder