即使数字相同,也总是打印出“抱歉”。为什么这个if语句不起作用?
import random
high_number = input("What is the maximum number?\nExample(20): ")
print('0-{}'.format(high_number))
guess = input("Guess the number: ")
high_number = int(high_number)
value = random.randint(0, high_number)
if value != guess:
print("Sorry.")
elif value == guess:
print("Hurray!")
答案 0 :(得分:0)
问题:input()
返回String而不是int
input()
会返回一个字符串,您正在将int value
与String guess
进行比较,这始终为false。将value!=guess
更改为value!=high_number
,它应该有效。
使用
high_number
,它是字符串guess
的整数值。