【问题标题】:Python random math's quiz generator program, need adjustmentsPython随机数学的测验生成器程序,需要调整
【发布时间】:2018-12-01 15:11:44
【问题描述】:

我正在尝试更改代码中的 3 件事。

  1. 使“答案”与用于“问题”的同一组 random.randomint 匹配。

  2. 让用户可以选择一个特定的运算符用于测验,而不是随机运算符。

  3. 对于减法运算符,确保第一个操作数大于第二个操作数,这样程序就不会给出否定答案。

感谢任何答案。 这是我的代码:

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 程序编写。 谢谢。

【问题讨论】:

  • 欢迎来到 SO。请花时间阅读How to Ask 以及该页面上的其他链接。
  • 为确保减法结果为正,您可以使用abs function。或者您可以先sort 值:small, big = sorted((4,3)); big - small
  • 您想将 questionsanswers 代码保存在不同的文件中吗?
  • 是的,问题和答案将在单独的文件中。第一个文件只是带有空格的随机问题,第二个文件是包含答案的相同随机问题。

标签: python windows file math


【解决方案1】:

1 使“答案”匹配用于“问题”的同一组 random.randomint。

您可以先建立一个列表,创建数字并将其用于问题和答案。

numbers = [(random.randint(1, 12), random.randint(1,12)) for _ in range(NoofQ)]

然后在问答中使用它:

for question_num in range(1,NoofQ +1): #i would prefer that question_num starts at 0
    ops = ['*','/','+','-']
    rand, rand2 = numbers[question_num-1] 

2 为用户提供一个选项,以选择用于测验的特定运算符,而不是随机运算符。

op = input("Please enter your operator (+, -, /, or *): ")

3 对于减法运算符,确保第一个操作数大于第二个操作数,这样程序就不会给出否定答案。

if operation == "-" and rand < rand2:
    rand, rand2 = rand2, rand

【讨论】:

  • 感谢您的回答,我设法得到了与“问题”和“答案”相同的数字。但是现在“答案”运算符与“问题”运算符不同,使得答案文件中的数学总和不同。有什么想法吗?
  • @Nathan 我相信您可以自己调整我为运营商提供的数字代码。这是相同的逻辑。
  • 我刚刚成功完成,非常感谢@Corentin Limier 的帮助。
【解决方案2】:

为确保减法结果为正,您可以使用abs function。或者您可以先sort 值:

answer = abs(4-3)
small, big = sorted((4,3))
answer = big - small. 

您创建了一个xyz_quiz.txt 文件,其中包含answers 代码所需的所有信息。阅读测验文件,对于每个问题,使用str 方法进行拆分和剥离,直到获得数学

>>> question = '1: 6 - 11 = ________'
>>> question, _ = question.split('=')
>>> question
'1: 6 - 11 '
>>> q_number, q = question.split(':')
>>> q_number
'1'
>>> q
' 6 - 11 '
>>> q = q.strip()
>>> q
'6 - 11'
>>>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2015-07-21
    • 2011-07-04
    相关资源
    最近更新 更多