导入随机
def g1to9():
a = random.randint(1,9)
wt = 0
b = input("Guess a number from 1 to 9:")
if a == b:
print("You guessed it correctly, it took you {} tries".format(wt))
while True:
if a != b:
print("You are wrong!")
b = input("Guess a number from 1 to 9:")
wt += 1
我正在尝试创建一个游戏"猜一个从1到9的数字"。但是当我运行它时,我检查所有数字,但a永远不等于b。我试图制作一个全局变量,但它没有用。我的代码出了什么问题?
答案 0 :(得分:3)
b类型为str
,因为它是输入。您需要先将其强制转换为int。你可以通过以下方式做到:
b = int(input("Guess a number from 1 to 9:"))