所以我正在制作一个数字猜测器,当我做对了之后,它会打印出"错误的";声明有人能帮我吗?
#randomly select number
import random
#Selects number
numbers = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50
#tell the user to pick a number
guess = input("I am think of a number between 1 and 50, Guess what number ")
num = random.choice(numbers) #selecting the number
#prints if you got it right
if num == guess:
print("Yes you go it! You rock.")
else:
print("WRONG! I was thinking of", num, "Try again! Better luck next time.")
应该做两件事
- 将
guess
强制转换为整数guess = int(input("..."))
或if num == int(guess)
。(这将解决您的问题( - 与其列出数字并随机选择,不如使用
random.randint()
。num = random.randint(1,50)
。您可以删除numbers
行,然后
猜测是输入的,即默认情况下它是一个字符串。
if num == int(guess):