您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

我的输入不等于答案吗?

我的输入不等于答案吗?

我假设您正在使用python3

代码的唯一问题是,您从中获取的值input()是字符串,而不是整数。因此,您需要进行转换。

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

现在您将输入作为整数,可以将其与 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())
@H_301_9@
  

          

解决方法

从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())
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!
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())
@H_301_9@
 

          

解决方法

从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())
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!
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())

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

编辑代码

从Unity
JS切换到Python稍有一些,关于这为什么行不通的一些细微之处使我难以理解。我最好的猜测是变量guess实际上是一个字符串,所以字符串5与整数5不同吗?这是正在发生的事情吗,或者如何解决这个问题。

现在您将输入作为整数,可以将其与 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())
@H_301_9@
 

          

解决方法

从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())
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!
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())

从Unity
JS切换到Python稍有一些,关于这为什么行不通的一些细微之处使我难以理解。我最好的猜测是变量guess实际上是一个字符串,所以字符串5与整数5不同吗?这是正在发生的事情吗,或者如何解决这个问题。

其他 2022/1/1 18:37:05 有233人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶