我正在尝试制作一个小程序,该程序将要求某人猜测被选出的数字。正确猜出数字后,程序应结束。当错误地猜出数字时,它应反复要求用户重试,直到猜出正确的答案为止。出于测试目的,我特意将随机值设置为始终为1。
import random
random_number = random.randint(1, 1)
guess_number = int(input('I have picked a random number. Can you guess what it is? '))
while guess_number != True:
int(input('I am sorry. That is not correct. Please try again. '))
else:
print('That is correct!')
我知道自己在做错的事情非常简单,找到答案后我很可能会踢自己,但对于我的一生,我现在无法解决。
答案 0 :(得分:1)
您有自己的random_number
,但没有将它与您的guess_number
进行比较。
您只能将guess_number
(一个整数)与True
(一个布尔值)进行比较,这显然是错误的。
请将您的代码与以下代码进行比较,并询问是否存在不清楚的地方:
import random
random_number = random.randint(1, 1)
guess_number = int(input('I have picked a random number. Can you guess what it is? '))
while guess_number != random_number:
guess_number = int(input('I am sorry. That is not correct. Please try again. '))
print('That is correct!')
此外,random.randint(1, 1)
始终返回1
。您可能想使用其他数字。
答案 1 :(得分:0)
几件事:
这是一个可行的解决方案,可以生成1到10之间的数字:
import random
random_number = random.randint(1, 10)
guess_number = -1
while True:
guess_number = int(input('I have picked a random number. Can you guess what it is? '))
if guess_number == random_number:
print('That is correct!')
break
print('I am sorry. That is not correct. Please try again. ')
答案 2 :(得分:0)
您需要将某些内容与您的guess_number进行比较。
while guess_number != random_number: