我一直得到相同的输出"You've guessed it wrong"



有人可以告诉我,如果有什么问题,我一直得到"你猜错了"作为我的输出 以及如何添加我可以进行的猜测数量

a=random.range(1,10)
b=input("enter a number: ") 
if a==b:print("the number you've guessed is correct") 
else:print("You've guessed it wrong")

这里的问题是内置函数input()总是返回一个字符串。然后稍后您将字符串与整数进行比较,因为random.range()总是返回一个字符串。一种解决方案是将您的输入转换为整数,如下所示。

a=random.range(1,10)
b=int(input("enter a number: "))

if a==b:
print("the number you've guessed is correct") 
else:
print("You've guessed it wrong")

编辑但是,我确实建议您对该输入进行某种验证,以便在字符串无法更改为整数时不会返回错误。

您正在比较字符串与整数,请尝试下面的代码而不是您的代码 它会罚款

a=random.range(1,10)
b=int(input("enter a number: "))
if a==b:
print("the number you've guessed is correct") 
else:
print("You've guessed it wrong")

b=input("enter a number: ")

要获取数字,请使用int(),因为input()接受字符串值。

用:

b = int(input("enter a number: "))

我认为你应该使用random.randrange()而不是random.range()

相关内容

最新更新