【问题标题】:How does my input not equal the answer?我的输入怎么不等于答案?
【发布时间】:2015-05-26 04:34:00
【问题描述】:

暂时从 Unity JS 切换到 Python,我无法理解为什么这不起作用。 我最好的猜测是变量guess实际上是一个字符串,所以字符串5和整数5不一样? 这是正在发生的事情吗?无论怎样解决这个问题。

import random
import operator

ops = {
    '+':operator.add,
    '-':operator.sub
}
def generateQuestion():
    x = random.randint(1, 10)
    y = random.randint(1, 10)
    op = random.choice(list(ops.keys()))
    a = ops.get(op)(x,y)
    print("What is {} {} {}?\n".format(x, op, y))
    return a

def askQuestion(a):
    guess = input("")
    if guess == a:
        print("Correct!")
    else:
        print("Wrong, the answer is",a)

askQuestion(generateQuestion())

【问题讨论】:

    标签: python string int


    【解决方案1】:

    我假设你使用的是 python3。

    您的代码的唯一问题是您从input() 获得的值是字符串而不是整数。所以你需要转换它。

    string_input = input('Question?')
    try:
        integer_input = int(string_input)
    except ValueError:
        print('Please enter a valid number')
    

    现在您将输入作为整数,您可以将其与a 进行比较

    编辑代码:

    import random
    import operator
    
    ops = {
        '+':operator.add,
        '-':operator.sub
    }
    def generateQuestion():
        x = random.randint(1, 10)
        y = random.randint(1, 10)
        op = random.choice(list(ops.keys()))
        a = ops.get(op)(x,y)
        print("What is {} {} {}?\n".format(x, op, y))
        return a
    
    def askQuestion(a):
        # you get the user input, it will be a string. eg: "5"
        guess = input("")
        # now you need to get the integer
        # the user can input everything but we cant convert everything to an integer so we use a try/except
        try:
            integer_input = int(guess)
        except ValueError:
            # if the user input was "this is a text" it would not be a valid number so the exception part is executed
            print('Please enter a valid number')
            # if the code in a function comes to a return it will end the function
            return
        if integer_input == a:
            print("Correct!")
        else:
            print("Wrong, the answer is",a)
    
    askQuestion(generateQuestion())
    

    【讨论】:

    • 对不起,我是菜鸟,但我不知道如何正确实施您的建议
    • @MattNewton 我能理解。不久前我还不能写出合适的 python 代码?
    【解决方案2】:

    是的,"5"5 不同,这绝对是正确的。您可以使用str(5)5 转换为字符串。另一种方法是将"5" 转换为int("5") 的整数,但该选项可能会失败,因此请更好地处理异常。

    因此,对程序的更改可能是例如以下:

    if guess == str(a):
    

    代替:

    if guess == a:
    

    另一种选择是将猜测转换为整数,如另一个答案中所述。

    编辑:这只适用于 Python 2.x 版本:

    但是,您使用的是input(),而不是raw_input()input() 如果您输入一个整数,则返回一个整数(如果您输入的文本不是有效的 Python 表达式,则会失败)。我测试了你的程序,它问What is 4 - 2?;我输入了2,它是Correct!,所以我看不出你有什么问题。

    您是否注意到如果您的程序询问What is 9 - 4?,您可以输入9 - 4,它会显示Correct!?那是因为你使用了input(),而不是raw_input()。同样,如果您键入例如c,您的程序因NameError 而失败

    不过,我会使用 raw_input(),然后将答案与 str(correct_answer) 进行比较

    【讨论】:

    • 我很确定 OP 正在使用没有 raw_input() 的 python3
    • 不知道你如何产生正确的结果 :) 我又试了一次,以确保我没有患上脑瘫,而且每种组合仍然出错。是的 raw_input() 对我不起作用,因为我使用的是 3.4。所以虽然我很重视你的回答,但我仍然卡住了:)
    猜你喜欢
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2018-09-28
    相关资源
    最近更新 更多