所以我有一个猜数字的游戏,我以前的所有代码都运行良好,你可能会看到我在屏幕上打印了随机生成的数字。但是,当我猜对了数字时,我仍然会被告知我错了?
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语句依赖于缩进,因此您应该使用适当的缩进来发布代码。
代码中:
a = input()
if a == num:
您正在将字符串与数字进行比较。您应该将a
强制转换为int
。
if int(a) == num: