我的程序没有正确地使用不同的运算符执行求和(乘法时加法,反之亦然)!

My program isn't executing sums using different operators properly (when multiplying it adds and vice-versa)!

我正在尝试制作一个程序,该程序生成一个由一系列随机问题组成的测验,在每种情况下使用任意两个数字和加法、减法或乘法。系统应该询问学生的 name,然后问 10 个问题,输出每个问题的答案是否正确,并得出最终分数,满分 10 分。

代码如下:

#Material 2: Task 1 (V1.0)

from random import randint
from random import choice

Name = input('What is your name? ')
Score = 0
QuestionNo = 0
Operators = ['+','-','*']
num1 = 0
num2 = 0


print ('\nWhat are the answers to the following questions?\n')

while QuestionNo != 11:
    num1 = randint(0,10)
    num2 = randint(0,10)
    Result = eval(str(num1) + (choice(Operators)) + str(num2))
    print (Result)
    Answer = int(input(str(num1) + (choice(Operators)) + str(num2) + '='))
    if Answer == Result:
        Score += 1
        print ('Well Done! That is correct!')
    elif Answer != Result:
        print ('Whoops! That is wrong!')
    QuestionNo += 1

    if QuestionNo == 10:
        print ('That is the end of the quiz '+ Name + '. Your score was: ' + str(Score) + ' out of 10!')
        if Score == 10:
            print ('Great job! You got everything right!')
        elif Score == 0:
            print ('You\'ve got to try harder next time!')
        break

问题是行 -

Result = eval(str(num1) + (choice(Operators)) + str(num2))
print (Result)
Answer = int(input(str(num1) + (choice(Operators)) + str(num2) + '='))

如您所见,您再次使用 choice(Operators),当要求用户输入时,这可以为不同的运算符提供用于计算结果的内容。您只需要获取 choice(Operators) 一次,并将其保存在变量中并将其用于评估和输入。

while QuestionNo != 11:
    num1 = randint(0,10)
    num2 = randint(0,10)
    opp = choice(Operators)
    Result = eval(str(num1) + (opp) + str(num2))
    print (Result)
    Answer = int(input(str(num1) + (opp) + str(num2) + '='))