我正在尝试更改代码中的 3 件事。
使“答案”与用于“问题”的同一组 random.randomint 匹配。
为用户提供一个选项来选择用于测验的特定运算符而不是随机运算符。
对于减法运算符,确保第一个操作数大于第二个操作数,这样程序就不会给出否定的答案。
任何答案表示赞赏。这是我的代码:
import random
print("Welcome to the maths quiz creator!")
CLASS = input("Please enter the class name: ")
NAME = input("Please enter your name: ")
NoofQ = int(input("How many questions for the quiz? "))
<--第一个问题文件-->
output_file = open('{}_quiz.txt'.format(CLASS), 'w')
print("Class:", CLASS)
print("Teacher:", NAME)
output_file.write("Class: ")
output_file.write(CLASS)
output_file.write("\nTeacher: ")
output_file.write(NAME)
for question_num in range(1,NoofQ +1):
ops = ['*','/','+','-']
rand=random.randint(1,12)
rand2=random.randint(1,12)
operation = random.choice(ops)
maths = eval(str(rand) + operation + str(rand2))
Questions = '\n {}: {} {} {} {} {}'.format(question_num, rand, operation, rand2, "=", "________")
print(Questions)
output_file.write(Questions)
output_file.close()
<--答案的第二个文件-->
output_file = open('{}_answers.txt'.format(CLASS), 'w')
print("Class:", CLASS)
print("Teacher:", NAME)
output_file.write("Class: ")
output_file.write(CLASS)
output_file.write("\nTeacher: ")
output_file.write(NAME)
for question_num in range(1, NoofQ +1):
ops = ['*','/','+','-']
rand=random.randint(1,12)
rand2=random.randint(1,12)
operation = random.choice(ops)
maths = eval(str(rand) + operation + str(rand2))
Answers = '\n {}: {} {} {} {} {}'. format(question_num, rand, operation, rand2, "=", int(maths))
print(Answers)
output_file.write(Answers)
output_file.close()
我对 Python 相当陌生,用 Pycharm 程序编写。谢谢你。
www说
繁花不似锦
相关分类