我需要编写一个程序来创建随机数学问题。
每个问题应具有以下格式:
<num> <op> <num> = ?
其中每个 num 代表一个介于 -50 和 50 之间的随机数,并且 op 是从四个基本数学运算符中随机选择的:+、-、/、*。
程序会将这个问题呈现给用户并等待答案。如果答案是正确的,程序应该祝贺用户并退出。如果答案不正确,程序应允许用户重试。用户将被允许继续尝试,直到给出正确答案。
每次编译代码时,我都会在“标志”处收到语法错误,但我不知道如何修复它。
def main():
import random
from operator import add, sub, mul, div
random.seed()
ops = (add, sub, mul, div)
op = random.choice(ops)
num1 = random.randint(-50,50)
num2 = random.randint(-50,50)
answer = op(num1, num2)
print("Welcome! Here is your practice problem:\n")
print(num1, op ,num2,"=?\n")
guess = int(input("What is your answer?\n")
flag = True
while flag:
guess = int(input("I’m sorry, that is not correct. Please try again.\n"))
if guess == answer:
flag = False
print("Congratulations! You have answered the problems correctly!\n)
main()