gpt4 book ai didi

Python变量名称.错误

转载 作者:行者123 更新时间:2023-12-01 04:57:55 26 4
gpt4 key购买 nike

在Python中,我正在编写一个程序来计算类(class)的成绩,考虑到类(class)作业的类型、每个类(class)的权重和分数。这是代码:

def get_initial_input():
try:
tests = int(input("How many tests are there? ")) #get number of tests
except ValueError:
print("Invalid number")
get_initial_input()
class_weight()

def class_weight():
print("What is the weighted percent of the class?")
if tests > 0: #<-- this is where the error is
try:
tests_weight = float(input("Tests: "))
except ValueError:
print("Invalid weight")
class_weight()

def main():
get_initial_input()

main()

每当我运行它时,我都会收到 builtins.NameError 发生消息:名称“tests”未定义 错误。该变量似乎是在程序的前面定义的,但由于某种原因似乎没有正确定义。任何帮助将不胜感激!

最佳答案

def get_initial_input():
try:
tests = int(input("How many tests are there? ")) #get number of tests
except ValueError:
print("Invalid number")
get_initial_input()
class_weight(tests)

def class_weight(tests):
print("What is the weighted percent of the class?")
if tests > 0: #<-- this is where the error is
try:
tests_weight = float(input("Tests: "))
except ValueError:
print("Invalid weight")
class_weight()

def main():
get_initial_input()

main()

只要通过测试就可以了。

关于Python变量名称.错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26955327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com