python - 我的除法方法不会运行。其他一切都很好
问题描述
超级困惑为什么我的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()
解决方案
global
我认为您对使用,用于修改局部范围内的全局变量有误解global
,因此尝试使correcttotal
global 没有用,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
,为什么不把它作为参数传递给你的函数呢,它会更易读,更容易跟踪。
推荐阅读
- java - 如何打印带有数字和星星的半金字塔?
- java - Pentaho 用户控制台、LDAP、TLS 协商失败
- react-native - 当我完成裁剪图像而没有任何错误日志时,应用程序会自动崩溃。我使用了世博会图像选择器
- go - 不同连接上不同主机之间的TCP连接
- sqlite - 根据记录的关联表中存在的值检索记录
- asp.net-core - 使用 .NET Core 通过 Sustainsys 进行 SAML2 登录的多个 IDP
- java - 向 PC 未安装 Java 的用户发送 Java 程序
- jenkins - 在 Jenkins Execute Shell 脚本块中转义单引号
- python - How to specify status code in Google Cloud Function response?
- python-3.x - cron 是否监视作业的退出代码,我有一个正在重新启动的 cron 作业