无法检查输入是否与变量匹配

时间:2017-02-14 22:29:21

标签: python python-3.x

我想生成1到6之间的数字并让用户猜它。 if input == variable:中的检查功能无效。

global count
count = 0   

 def guess():
        a = random.randint(1,6)
        print (a)
        ffs =input ("whats my nr?")
        if ffs == (a):
            print ("Correct")
            sys.exit()
        else:
            global count
            print ("guess again")
            count +=1
            print ("you have attempts remaining")
    def mainz():
        while count < 6:
            guess()
    mainz()

2 个答案:

答案 0 :(得分:0)

global count
count = 0
def guess():
    a = random.randint(1,6)
    print (a)
    ffs =input ("whats my nr?")
    if ffs == str(a):
        print ("Correct")
        sys.exit()
    else:
        global count
        print ("guess again")
        count +=1
        print ("you have attempts remaining")
def mainz():
    while count < 6:
        guess()
mainz()

需要str(a)

答案 1 :(得分:0)

首先,您的mainz()函数和mainz()调用都在guess()函数中,但我猜这只是格式化错误。真正的错误在于输入返回一个你想要与int进行比较的字符串。我建议只是这样做:

ffs = int(input("What's my nr? ") )