该程序假定生成一个介于0到10之间的随机数,然后提示用户在5次尝试中猜测它。但是程序连续输出“您猜错了”。我已经尝试了很多次,只是不断重复打印“您猜错了”五次。当用户拥有正确的号码时,如何提示它说“您猜对了”?
import random
def guess():
randNum = random.randint(0,10)
count = 0
while count < 5:
count = count + 1
number1 = input("Guess A Number")
if number1 != randNum:
print(" You Guessed incorrect")
else:
print(" You are Correct")
答案 0 :(得分:0)
缩进错误。并且您需要将猜测转换为int
才能将其与randNum
进行比较。您可能想在正确答案后break
。试试这个:
import random
def guess():
randNum = random.randint(0,10)
count = 0
while count < 5:
count = count + 1
number1 = int(input("Guess A Number: "))
if number1 != randNum:
print(" You Guessed incorrect")
else:
print(" You are Correct")
break
print("The number was: " + str(randNum))
guess()