<tfoot id='XaAOX'></tfoot>

    <small id='XaAOX'></small><noframes id='XaAOX'>

      • <bdo id='XaAOX'></bdo><ul id='XaAOX'></ul>
      <legend id='XaAOX'><style id='XaAOX'><dir id='XaAOX'><q id='XaAOX'></q></dir></style></legend>
      <i id='XaAOX'><tr id='XaAOX'><dt id='XaAOX'><q id='XaAOX'><span id='XaAOX'><b id='XaAOX'><form id='XaAOX'><ins id='XaAOX'></ins><ul id='XaAOX'></ul><sub id='XaAOX'></sub></form><legend id='XaAOX'></legend><bdo id='XaAOX'><pre id='XaAOX'><center id='XaAOX'></center></pre></bdo></b><th id='XaAOX'></th></span></q></dt></tr></i><div id='XaAOX'><tfoot id='XaAOX'></tfoot><dl id='XaAOX'><fieldset id='XaAOX'></fieldset></dl></div>

    1. 我的输入怎么不等于答案?

      时间:2023-09-11
      <legend id='BjYZQ'><style id='BjYZQ'><dir id='BjYZQ'><q id='BjYZQ'></q></dir></style></legend>
      <i id='BjYZQ'><tr id='BjYZQ'><dt id='BjYZQ'><q id='BjYZQ'><span id='BjYZQ'><b id='BjYZQ'><form id='BjYZQ'><ins id='BjYZQ'></ins><ul id='BjYZQ'></ul><sub id='BjYZQ'></sub></form><legend id='BjYZQ'></legend><bdo id='BjYZQ'><pre id='BjYZQ'><center id='BjYZQ'></center></pre></bdo></b><th id='BjYZQ'></th></span></q></dt></tr></i><div id='BjYZQ'><tfoot id='BjYZQ'></tfoot><dl id='BjYZQ'><fieldset id='BjYZQ'></fieldset></dl></div>

    2. <small id='BjYZQ'></small><noframes id='BjYZQ'>

              <tbody id='BjYZQ'></tbody>
                <bdo id='BjYZQ'></bdo><ul id='BjYZQ'></ul>
                <tfoot id='BjYZQ'></tfoot>

                本文介绍了我的输入怎么不等于答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                Switching from Unity JS to Python for a bit, and some of the finer points elude me as to why this does not work. My best guess is that the variable guess is actually a string, so string 5 is not the same as integer 5? Is this what is happening and either way how does one go about fixing this.

                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 {} {} {}?
                ".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())
                

                推荐答案

                我假设你使用的是python3.

                I am assuming you are using python3.

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

                The only problem with your code is that the value you get from input() is a string and not a integer. So you need to convert that.

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

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

                Now you have the input as a integer and you can compare it to 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 {} {} {}?
                ".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())
                

                这篇关于我的输入怎么不等于答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:ValueError: int() 以 10 为底的无效文字:'stop' 下一篇:如何在python中将有符号整数转换为无符号整数

                相关文章

                最新文章

                <small id='5tewi'></small><noframes id='5tewi'>

                <i id='5tewi'><tr id='5tewi'><dt id='5tewi'><q id='5tewi'><span id='5tewi'><b id='5tewi'><form id='5tewi'><ins id='5tewi'></ins><ul id='5tewi'></ul><sub id='5tewi'></sub></form><legend id='5tewi'></legend><bdo id='5tewi'><pre id='5tewi'><center id='5tewi'></center></pre></bdo></b><th id='5tewi'></th></span></q></dt></tr></i><div id='5tewi'><tfoot id='5tewi'></tfoot><dl id='5tewi'><fieldset id='5tewi'></fieldset></dl></div>
                1. <tfoot id='5tewi'></tfoot>

                  <legend id='5tewi'><style id='5tewi'><dir id='5tewi'><q id='5tewi'></q></dir></style></legend>

                    <bdo id='5tewi'></bdo><ul id='5tewi'></ul>