数学测验任务Python

2024-09-30 04:38:47 发布

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

我被安排了一项数学测验程序。基本代码是给我的,我被设置了一系列的挑战,使之更复杂。我检查最终分数是否在某个范围内的方法不能正常工作,指令是检查用户的得分是否在66%或更高,如果是这样,程序应该打印“做得好!”。如果用户的分数在33%到66%之间,程序应该打印“你需要更多的练习”,如果他们的分数低于33%,那么程序将打印“请向你的数学老师寻求帮助”。然而,这并没有发生,而且有一些奇怪的结果。感谢任何帮助。在

代码如下:

from random import randint 

while True:

    correct = 0

    userrange = int(input("How many questions would you like?\n"))

    difficulty = input("Choose your difficulty: beginner, intermediate or advanced?\n")

    if difficulty == "beginner":
            no = 10
    if difficulty == "intermediate":
            no = 25
    if difficulty == "advanced":
            no = 100


    qtype = input("Would you like to do addition, subtraction or multiplication?\n")    

    if qtype == "addition":
        for i in range(userrange):
            n1 = randint(1, no)
            n2 = randint(1, no)
            prod = n1 + n2
            lo = "plus"
    if qtype == "subtraction":
        for i in range(userrange):
            n1 = randint(1, no)
            n2 = randint(1, no)
            prod = n1 - n2
            lo = "minus"
    if qtype == "multiplication":
        for i in range(userrange):
            n1 = randint(1, no)
            n2 = randint(1, no)
            prod = n1 * n2
            lo = "times"


    ans = int(input("What's %d %s %d?"  % (n1 ,lo ,n2)))
    if ans == prod:
        print ("That's right -- well done.\n")
        correct = correct + 1
    else:
        print ("No, I'm afraid the answer is %d.\n" % prod)



    print ("\nI asked you %d questions.  You got %d of them right." %(userrange, correct))
    if correct >= (userrange%3)*2:
        print ("Well done!")
    elif (userrange%3) < correct < (userrange%3)*2:
        print ("You need more practice")
    else:
        print ("Please ask your maths teacher for help!")


    try_again = int(input("\nPress 1 to try again, press 0 to exit.\n "))
    if try_again == 0:
        break

Tags: no程序loforinputifprodprint
1条回答
网友
1楼 · 发布于 2024-09-30 04:38:47

你很接近。。。你只需要稍微移动一下循环的位置,这样它就可以捕捉到问题的实际问题。在

现在的情况是,你只是在你产生问题的那部分循环。在

我将在这里发布整个代码,因为有一些缩进更改以支持新循环:

from random import randint 

while True:
    correct = 0

    userrange = int(input("How many questions would you like?\n"))

    difficulty = input("Choose your difficulty: beginner, intermediate or advanced?\n")

    if difficulty == "beginner":
            no = 10
    if difficulty == "intermediate":
            no = 25
    if difficulty == "advanced":
            no = 100

    qtype = input("Would you like to do addition, subtraction or multiplication?\n")    

    for i in range(userrange):  # Move the loop to start here, instead of under each if / elif
        n1 = randint(1, no)  # We can go ahead and generate our random numbers here also
        n2 = randint(1, no)
        if qtype == "addition":
            prod = n1 + n2
            lo = "plus"
        elif qtype == "subtraction":
            prod = n1 - n2
            lo = "minus"
        elif qtype == "multiplication":
            prod = n1 * n2
            lo = "times"


        ans = int(input("What's %d %s %d?"  % (n1 ,lo ,n2)))  # We also need to move the question into the loop, so that it is asked each time
        if ans == prod:
            print ("That's right   well done.\n")
            correct = correct + 1
        else:
            print ("No, I'm afraid the answer is %d.\n" % prod)



    print ("\nI asked you %d questions.  You got %d of them right." %(userrange, correct))
    if correct >= (userrange%3)*2:
        print ("Well done!")
    elif (userrange%3) < correct < (userrange%3)*2:
        print ("You need more practice")
    else:
        print ("Please ask your maths teacher for help!")


    try_again = int(input("\nPress 1 to try again, press 0 to exit.\n "))
    if try_again == 0:
        break

相关问题 更多 >

    热门问题