Python:如何让Python计算一个和以确保输入是正确的?

2024-05-08 03:20:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让python问10个问题,用户必须输入他们的答案。不过,我也希望python通过使用下面的代码来说明这是否正确,但这不起作用,只会进入下一个问题。有人能告诉我为什么吗?或者我需要改变什么?另外,我该如何使用我拥有的变量和while循环让这个ask 10问题具体化呢?你知道吗

import time
import random
question = 0
score = 0
name = input("What is your full name?")
print ("Hello " + name, "welcome to The Arithmetic Quiz")
time.sleep(2)
operand1 = list(range(2, 12))
operators = ["+"]
operand2 = list(range(2, 12))

while question < 10:
    user_answer=int(input(str(random.choice(operand1)) + random.choice(operators) + str(random.choice(operand2))))
    if operators=='+':
        expected_answer==operand1 + operand2
        if user_answer==expected_answer:
            print('This is correct!')
            score = score + 1
            question = question + 1
            time.sleep(2)
        else:
            print('This is incorrect!')
            question = question + 1
            time.sleep(2)

Tags: answernameimporttimeissleeprandomscore
2条回答

以下是解决您问题的方法:

import time
import random

question = 0
score = 0
name = input("What is your full name?")
print ("Hello " + name, "welcome to The Arithmetic Quiz")
time.sleep(2)
operand1 = list(range(2, 12))
operand2 = list(range(2, 12))

while question < 10:
    num1 = random.choice(operand1)
    num2 = random.choice(operand2)
    print(str(num1) + "+" + str(num2))
    user_answer = int(input())
    expected_answer = int(num1) + int(num2)
    if user_answer == expected_answer:
        print('This is correct!!')
        score = score + 1
        question = question + 1
    else:
        print('This is incorrect!!')
        question = question + 1

print("\nYour score is " + str(score))

这里不需要operands变量,而是可以将+运算符作为字符串本身传递。 另外,由于操作数1和操作数2是作为列表传递的,而不是作为int传递的,所以预期的\u answer变量无法解析求和

下面是代码的错误

import time
import random
question = 0
score = 0
name = input("What is your full name?")
print ("Hello " + name, "welcome to The Arithmetic Quiz")
time.sleep(2)
operand1 = list(range(2, 12))
#Choice works fine with ranges 
#No need to transform it with a list
operators = ["+"]
operand2 = list(range(2, 12))
#Using the for loop is more Pythonic
while question < 10: 
    user_answer=int(input(str(random.choice(operand1)) + random.choice(operators) + str(random.choice(operand2))))
    if operators=='+': ##Over here you were comparing a list to a str
        expected_answer==operand1 + operand2 ##This is a boolean operator not an int value
        if user_answer==expected_answer:
            print('This is correct!')
            score = score + 1
            question = question + 1
            time.sleep(2)
        else:
            print('This is incorrect!')
            question = question + 1
            time.sleep(2)

正如Kupiakos所说,有很多方法可以优化代码,他已经介绍了其中的大部分。我将指出解决上述问题的方法。你知道吗

import time
from random import choice, randint
question, score = 0, 0

name = input("What is your full name?\n>>> ")
print ("Hello {} welcome to The Arithmetic Quiz\n".format(name))
time.sleep(2)

for _ in range(10):
    operand1, operand2 = [randint(2, 12) for _ in range(2)]
    op = choice(['+'])##You have to store the value so that you can compare it later
    user_answer=int(input('{0}{2}{1}\n>>> '.format(operand1, operand2, op) ))
    if op == '+':
        expected_answer = operand1 + operand2
        if user_answer == expected_answer:
            print('This is correct!')
            score += 1
            question += 1
            time.sleep(2)
        else:
            print('This is incorrect!')
            question = question + 1
            time.sleep(2)
print('Your score is: {} points'.format(score))

祝你的学生好运。你知道吗

相关问题 更多 >