储存正确答案并保存

2024-10-01 19:22:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉堆栈溢出,最近才开始学习Python编程。我目前使用的是python2.7,因此与pythonv3相比,我的代码会有所不同。在

总之,我决定写一个程序,让我的侄子和侄女试用一下。创建的程序是一个数学测试。在

代码可能不是最好的或紧凑的,因为它应该是,但这是我的第一次尝试。在

我附上了我迄今为止在这里键入的代码。请让我知道你的想法和任何反馈。在

我的主要问题是:

  • 我想将用户输入的正确答案存储在变量中。然后我希望能够输出用户在测试结束时正确回答的问题的数量。在

例如,如果用户决定接受5个问题集的挑战,得到3个正确答案,我希望程序输出,你得到3/5的正确答案。我有一种感觉,这将是一个非常简单的事情,但我还没有想到。在

我试着创建一个变量,比如answerscorect=0,然后只要answerscorect+=1,每次答案正确时都会递增,但我似乎无法使它起作用,所以我就不做了。在

对此有什么建议吗?在

from sys import exit

随机导入 从随机导入randint 导入数学

def addition(): problems=int(input(“你想解决多少个问题?\n“))

^{pr2}$

def reduction(): problems=int(input(“你想解决多少个问题?\n“))

random.seed()
count = 0
answersRight = 0

while count < problems:
    num_1 = randint(1,12)
    num_2 = randint(1,12)

    userAnswer = int(input("What is " + str(num_1) + "-" + str(num_2) + "? "))
    generatedAnswer = (num_1 - num_2)


    if userAnswer == generatedAnswer:
        print "Correct"
        answersRight += 1
    else:
        print "Sorry, the answer is ", generatedAnswer, "\n"
    count += 1


    print "running score of answers right ", answersRight, " out of ", problems

print "Would you like to solve more problems?"
repeatAnswer = raw_input("> ")

if repeatAnswer == "Yes" or repeatAnswer == "Y" or repeatAnswer == "YES":
    start()
elif repeatAnswer == "No" or repeatAnswer == "N" or repeatAnswer == "NO":
    print "Thank you for completing the test anyway..."
    exit()
else:
    print "You have not entered a valid response, goodbye!"
    exit()

def multiply(): problems=int(input(“你想解决多少个问题?\n“))

random.seed()
count = 0
answersRight = 0

while count < problems:
    num_1 = randint(1,12)
    num_2 = randint(1,12)

    userAnswer = int(input("What is " + str(num_1) + "x" + str(num_2) + "? "))
    generatedAnswer = (num_1 * num_2)

    if userAnswer == generatedAnswer:
        print "Correct"
        answersRight += 1
    else:
        print "Sorry, the answer is ", generatedAnswer, "\n"
    count += 1


    print "running score of answers right ", answersRight, " out of ", problems

print "Would you like to solve more problems?"
repeatAnswer = raw_input("> ")

if repeatAnswer == "Yes" or repeatAnswer == "Y" or repeatAnswer == "YES":
    start()
elif repeatAnswer == "No" or repeatAnswer == "N" or repeatAnswer == "NO":
    print "Thank you for completing the test anyway..."
    exit()
else:
    print "You have not entered a valid response, goodbye!"
    exit()

def divide(): problems=int(input(“你想解决多少个问题?\n“))

random.seed()
count = 0
answersRight = 0

while count < problems:
    num_1 = randint(1,12)
    num_2 = randint(1,12)

    userAnswer = int(input("What is " + str(num_1) + "/" + str(num_2) + "? "))
    generatedAnswer = (num_1 / num_2)

    if userAnswer == generatedAnswer:
        print "Correct"
        answersRight += 1
    else:
        print "Sorry, the answer is ", generatedAnswer, "\n"
    count += 1


    print "running score of answers right ", answersRight, " out of ", problems

print "Would you like to solve more problems?"
repeatAnswer = raw_input("> ")

if repeatAnswer == "Yes" or repeatAnswer == "Y" or repeatAnswer == "YES":
    start()
elif repeatAnswer == "No" or repeatAnswer == "N" or repeatAnswer == "NO":
    print "Thank you for completing the test anyway..."
    exit()
else:
    print "You have not entered a valid response, goodbye!"
    exit()

def start(): #欢迎留言 print“欢迎来到这个叫做Mathemagica的神奇游戏!” 打印“我希望你准备好迎接一个惊人的挑战:D” 打印“我是MATHEMAGIC先生,我是来测试你的能力的” 打印“…” 打印“所以,首先请输入您的姓名:” 名称=原始输入(“>;”) 打印“右”,name,“请输入您的年龄:” 年龄=原始输入(“>;”) 打印“谢谢!那么,“,name,”你准备好开始你的数学考试了吗?(是/否)” 响应=原始输入(“>;”)

# Response Dependant directing to a particular function to be CALLED
if response == "Yes" or response == "YES" or response == "Y" or response == "y":
    print """Which test would you like to take first? Please select an option below:
        \t1. Addition
        \t2. Subtraction
        \t3. Multiplication
        \t4. Division
        """
    # Resonse here
    problemSolver = raw_input("> ")
    # Evaluating what option has been selected
    if problemSolver == "1":
        addition()
    elif problemSolver == "2":
        subtraction()
    elif problemSolver == "3":
        multiply()
    elif problemSolver == "4":
        divide()
    else:
        print "You have not entered a valid option, Goodbye!"
        exit()
elif response == "No" or response == "NO" or response == "N" or response == "n":
    print "That's fine. Have a good day :)"
    exit(0)
else:
    print"You have not entered a valid response, Bye"

开始()


Tags: orinputifresponsecountexitelsenum
1条回答
网友
1楼 · 发布于 2024-10-01 19:22:22

您需要注意将语句放在何处,以确定它们是否在ifs和循环中。以下是正确的加法代码,其他代码类似:

def addition():
    problems = int(input("How many problems do you want to solve?\n"))

    random.seed()
    count = 0
    answersRight = 0

    while count < problems:
        num_1 = randint(1,12)
        num_2 = randint(1,12)

        userAnswer = int(input("What is " + str(num_1) + "+" + str(num_2) + "? "))
        generatedAnswer = (num_1 + num_2)

        if userAnswer == generatedAnswer:
            print "Correct"
            answersRight += 1
        else:
            print "Sorry, the answer is ", generatedAnswer, "\n"
        count += 1

        print "running score of answers right ", answersRight, " out of ", problems

    start()

相关问题 更多 >

    热门问题