为什么 elif 语句优先于我的 if 语句?
Why is the elif statement taking priority over my if statement?
所以我有一个数字猜谜游戏,我以前的所有代码都运行良好,您可能会看到我在屏幕上打印了随机生成的数字。但是当我猜对数字时,我仍然被告知我错了?
num = randrange(0, 100)
if num > 0 and num < 50:
print("The number is greater than 0 but smaller than 50")
elif num > 50 and num < 100:
print("The number is smaller than 100 but greater than 50")
print(num)
print('Start Guessing')
a = input()
a = input()
if a == num:
print ("You got the correct number, woohoo")
elif num > 0 and num < 20:
print ("Hint: The number is bigger than 0 but smaller than 20")
elif num > 20 and num < 40:
print ('Hint: The number is bigger than 20 but smaller than 40')
elif num > 40 and num < 60:
print ('Hint: The number is bigger than 40 but smaller than 60')
elif num > 60 and num < 80:
print ('Hint: The number is bigger than 60 but smaller than 80')
elif num > 80 and num < 100:
print ('Hint: The number is bigger than 80 but smaller than 100')
抱歉代码看起来很奇怪,我以前从未使用过这个网站。
您的代码似乎是 Python。您应该在标签中指出这一点。此外,由于 Python if 语句依赖于缩进,因此您应该 post 您的代码具有适当的缩进。
代码中:
a = input()
if a == num:
您正在将字符串与数字进行比较。您应该将 a
转换为 int
.
if int(a) == num:
所以我有一个数字猜谜游戏,我以前的所有代码都运行良好,您可能会看到我在屏幕上打印了随机生成的数字。但是当我猜对数字时,我仍然被告知我错了?
num = randrange(0, 100)
if num > 0 and num < 50:
print("The number is greater than 0 but smaller than 50")
elif num > 50 and num < 100:
print("The number is smaller than 100 but greater than 50")
print(num)
print('Start Guessing')
a = input()
a = input()
if a == num:
print ("You got the correct number, woohoo")
elif num > 0 and num < 20:
print ("Hint: The number is bigger than 0 but smaller than 20")
elif num > 20 and num < 40:
print ('Hint: The number is bigger than 20 but smaller than 40')
elif num > 40 and num < 60:
print ('Hint: The number is bigger than 40 but smaller than 60')
elif num > 60 and num < 80:
print ('Hint: The number is bigger than 60 but smaller than 80')
elif num > 80 and num < 100:
print ('Hint: The number is bigger than 80 but smaller than 100')
抱歉代码看起来很奇怪,我以前从未使用过这个网站。
您的代码似乎是 Python。您应该在标签中指出这一点。此外,由于 Python if 语句依赖于缩进,因此您应该 post 您的代码具有适当的缩进。
代码中:
a = input()
if a == num:
您正在将字符串与数字进行比较。您应该将 a
转换为 int
.
if int(a) == num: