首页 > 解决方案 > 我的除法方法不会运行。其他一切都很好

问题描述

超级困惑为什么我的division()方法无法运行,其他一切都很好。我知道这是多么愚蠢的简单,但我被搞得一头雾水。我还包括其他方法,但由于不需要,我将它们删除,我的其他方法可以使用相同的确切代码,但除法它无法运行。

def TudorSelect():
    TaskSelect = input(
        "Please select one of the following tasks to practice; Addition, Subtraction, Multiplication, Divison:    "
    )
    global total
    global difficulty
    difficulty = int(
        input("Please select the max value range of your values: 0 - "))
    print(chr(13))
    total = int(input("Total practice questions for this task:    "))
    if TaskSelect == "Addition":
        addition()
    elif TaskSelect == "Subtraction":
        subtraction()
    elif TaskSelect == "Multiplication":
        multiplication()
    elif TaskSelect == "Divison":
        division()

def division():
    for num in range(total):
        global correcttotal
        randvalue1 = random.randint(1, difficulty)
        randvalue2 = random.randint(1, difficulty)
        correctvalue = (randvalue1 / randvalue2)
        print(str(randvalue1), " / ", str(randvalue2))
        uservalue = input()
        if uservalue == str(correctvalue):
            print("Correct")
            correcttotal = correcttotal + 1
        else:
            print("False")
            print("Correct answer is ", str(correctvalue))
    CorrectRate = ((correcttotal) / (total)) * 100
    print("Your total mark is ", str(CorrectRate), "%")
    TudorSelect()

标签: pythonpython-3.xpython-2.7

解决方案


global我认为您对使用,用于修改局部范围内的全局变量有误解global,因此尝试使correcttotalglobal 没有用,correcttotal仅在您的函数中声明。

因此,要解决此问题,您需要global从循环中删除关键字,并在correcttotal循环之外定义累加器,它需要在循环之外处于活动状态才能正确累加:

import random
def TudorSelect():
    TaskSelect = input(
        "Please select one of the following tasks to practice; Addition, Subtraction, Multiplication, Divison:    "
    )
    global total
    global difficulty
    difficulty = int(
        input("Please select the max value range of your values: 0 - "))
    print(chr(13))
    total = int(input("Total practice questions for this task:    "))

    if TaskSelect == "Addition":
        addition()
    elif TaskSelect == "Subtraction":
        subtraction()
    elif TaskSelect == "Multiplication":
        multiplication()
    elif TaskSelect == "Divison":
        division()

def division():
    correcttotal = 0
    for num in range(total):
        randvalue1 = random.randint(1, difficulty)
        randvalue2 = random.randint(1, difficulty)
        correctvalue = (randvalue1 / randvalue2)
        print(str(randvalue1), " / ", str(randvalue2))
        uservalue = input()
        if uservalue == str(correctvalue):
            print("Correct")
            correcttotal = correcttotal + 1
        else:
            print("False")
            print("Correct answer is ", str(correctvalue))
    CorrectRate = ((correcttotal) / (total)) * 100
    print("Your total mark is ", str(CorrectRate), "%")
    TudorSelect()

TudorSelect()

现在当你像这样运行它

Please select one of the following tasks to practice; Addition, Subtraction, Multiplication, Divison:    Divison
Please select the max value range of your values: 0 - 200

Total practice questions for this task:    5
117  /  115
20
False
Correct answer is  1.017391304347826
50  /  6
20
False
Correct answer is  8.333333333333334
64  /  158
10
False
Correct answer is  0.4050632911392405
61  /  109
1.5
False
Correct answer is  0.5596330275229358
66  /  18
3.667
False
Correct answer is  3.6666666666666665
Your total mark is  0.0 %

它工作正常,而且你的答案检查非常严格,我不是一台超级计算机,至少可以在答案的浮点部分达到那种准确度,我认为你需要先对答案进行四舍五入,然后再进行比较与学生的回答。

最后一点,我真的不推荐使用global,为什么不把它作为参数传递给你的函数呢,它会更易读,更容易跟踪。


推荐阅读