需要帮助:choolproject:我正在编写一个python程序,在其中您应该能够回答问题,但是我正在努力使其处于[保持状态]

问题描述 投票:-4回答:1

我正在编写一个python程序,在其中您应该能够回答数学问题。我正在尝试修复该程序为我计算一些简单的总和,但这并没有真正解决。我无法获得变量“ a”作为答案。 -程序已更新

Python代码:

“ def测试():导入数学b = 1c = 5a = math.sqrt(c-b)课程问题:def init(自我,问题,答案,答案2):self.question =问题self.answer =答案self.answer2 = answer2

    questions_list =[
    ["Solve the equation x**2 + 1 = 5. \n \nRestriction: you can not use spaces in your answer. \nNote your answer in the following form if necessery: x=.. v √x v counter/denominator v x**2.",
    ("x=" + str(a) + "vx=-" + str(a)), ("x=-" + str(a) + "vx=" + str(a))],
    ]
    questions = []

    for item in questions_list:
        questions.append(Questions(item[0],item[1],item[2]))

    def run_test(questions):
        for question in questions :
            print(questions.question)
            answer = input("\nAnswer: ")
            if answer == question.answer or answer == question.answer2:
                print("\nCorrect!\n")
                break
            if answer == ("x="+str(a)):
                print ("\nFalse, don’t forget that a negative x-value can also come out.\n\n")
                Test()
                break
            if answer == ("x=-"+str(a)):
                print ("\nFalse, don’t forget that a positive x-value can also come out.\n\n")
                Test()
                break
            if answer == ("x=√2") :
                print ("\nFalse, you haven taken the root twice of 5 - 1 = 4.\n\n")
                Test()
                break
            if answer == ("x=√2vx=-√2") :
                print ("\nFalse, you haven taken the root twice of 5 - 1 = 4.\n\n")
                Test()
                break
            if answer == ("x=-√2vx=√2") :
                print ("\nFalse, you haven taken the root twice of 5 - 1 = 4.\n\n")
                Test()
                break
            if answer == ("x=√4vx=-√4") :
                print ("\nFalse, you can work out your answer even further.\n\n")
                Test()
                break
            if answer == ("x=-√4vx=√4") :
                print ("\nFalse, you can work out your answer even further.\n\n")
                Test()
                break
            if answer == ("x=√4") :
                print ("\nFalse, you can work out your answer even further.\n\n")
                Test()
                break
            if answer == ("x=-√4") :
                print ("\nFalse, you can work out your answer even further.\n\n")
                Test()
                break
            else :
                print ("nFalse, answer not detected. \nTry again! \n\n")
                Test()

    run_test(questions)

请快速帮助,我没有多少时间了。所以你可能是我的救星!

python python-3.x
1个回答
0
投票

您已经很好地将答案存储在问题类中,但是后来您再也没有使用它,

        if answer == ("x=" + str(a) + "vx=-" + str(a)):
            print("\nCorrect!\n")
            break
        if answer == ("x=-" + str(a) + "vx=" + str(a)):
            print("\nCorrect!\n")
            break

应该是

 if answer == question.answer or answer == question.answer2:
      print("\nCorrect!\n")
      break

但是在此之前,您需要更改在for循环中给出的元素名称,因为您要重新分配现有变量

for questions in questions :

更改为

for question in questions:

并在for循环内将questions的任何用法更新为question


def run_test(questions):
    for question in questions :
    # for questions in questions :

        print(question.question)
        # print(questions.question)


        answer = input("\nAnswer: ")
        if answer == question.answer or answer == question.answer2:
        # if answer == ("x=" + str(a) + "vx=-" + str(a)):

            print("\nCorrect!\n")
            break
© www.soinside.com 2019 - 2024. All rights reserved.