从python的猜谜游戏中,我无法让自己得到正确的答案

时间:2016-08-11 04:45:18

标签: python

这是我的代码:

from sys import exit
import random
number = random.randint(1, 10)
count = 0

def guess():
    
    print ("Input a number 1 - 10")
    guess = input()

    if guess == "I give up":
        print ("The correct number was", number,"!")
        print ("You tried", count, "times before giving up!")
        exit(0)
    else:
        if guess == (number):
            print ("CORRECT!")
            print (count, "failed attempts.")
            exit(0)
        else:
            print ("WRONG!")
            print ("Try again!")
            global count
            count += 1
    
while True:
    guess()

如果我运行这个,我可以继续猜测,但永远不会得到正确的数字。我说我放弃了,它给了我正确的号码。但我已经猜到了这个数字,所以我不知道是什么问题。

1 个答案:

答案 0 :(得分:0)

您将输入视为字符串而不是整数。因此,您可能会将"5"5进行比较,这是错误的。相反,请致电if int(guess) == (number)