我的除法无法运行。其他一切都很好

2024-09-30 10:29:50 发布

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

超级困惑为什么我的division()方法不能运行,其他一切都很好。我知道这有多愚蠢,但我非常困惑。我还包括了其他方法,但由于不需要,iv删除了它们,我的其他方法可以使用相同的精确代码,但除法无法运行

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()

Tags: 方法inputdefglobaldivisiontotalprintelif
1条回答
网友
1楼 · 发布于 2024-09-30 10:29:50

我认为您对使用global有一个误解,global用于修改局部范围中的全局变量,因此尝试使correcttotal全局是没有用的,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,为什么不把它作为一个参数传递给你的函数呢?相反,它会更可读,更容易跟踪

相关问题 更多 >

    热门问题