数学测验任务Python

时间:2015-11-17 14:10:07

标签: python math

我已经设定了编程数学测验的任务。基本代码是给我的,我设置了一系列挑战,使其更复杂。我检查最终得分是否在一定范围内的方法不能正常工作,说明是检查用户是否得分为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

1 个答案:

答案 0 :(得分:0)

你关闭......你只需要稍微移动你的循环定位,以便它能够捕捉问题的实际问题。

现在看来,你只是在你生成问题的部分循环。

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

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
相关问题