NameError: name 'InputNumber' is not defined
NameError: name 'InputNumber' is not defined
抱歉,我是 python 的新手,我知道这可能很容易解决,但我已经被困了好久。
这是我的代码:
CountFromNum = InputNumber("Which number would you like to count down from? ")
print("This is Joe's Multiple Program Loader, hope you enjoy.")
print("Would you like to:")
print("(a) Count down from a certain number.")
print("(b) Print your name a certain amount of times.")
StartQuestion = input("(c) make a list with a custom length. ")
StartQuestionOptions='abc'
while StartQuestion not in StartQuestionOptions :
StartQuestion=input("Please enter the letter a, b or c: ")
if StartQuestion in StartQuestionOptions :
break
else :
StartQuestion=input("Please enter the letter a, b or c: ")
#Module A: P1
if StartQuestion == 'a':
print("You have selected: Option a")
def InputNumber(message):
while True:
try:
Userinput = int(input(message))
except ValueError:
print("Not a number! Please try again.")
continue
else:
return Userinput
break
CountFromNum = InputNumber("Which number would you like to count down from? ")
#Module B: P1
if StartQuestion == 'b':
print("You have selected: Option b")
Username = input("Please enter your name: ")
# Module B: P2
def InputNumber(message):
while True:
try:
Userinput = int(input(message))
except ValueError:
print("Please enter a number, try again.")
continue
else:
return Userinput
break
#Module B: P3
NameRepeatValue = inputNumber("How many times do you want to repeat your name? ")
while NameRepeatValue > 0:
print (Username)
NameRepeatValue -=1
while NameRepeatValue > 0:
exit()
#Module C: P1
if StartQuestion == 'c':
print("You have selected: Option c")
if StartQuestion == 'abc':
print("Stop trying to break my program.")
每当我做某事时总是出错,请帮忙。
主要问题是您试图在函数定义之前使用它。将 InputNumber
的定义移至文件顶部;或至少在使用之前。
您应该开始练习的另一个修复方法是将所有代码包装在函数中。您在顶层拥有的所有不是函数定义的代码最好都应该放入 main
函数或类似的东西中。这将在这里解决您的问题,并且如果您开始使用 REPL 进行开发,还可以简化以后的开发。通过将所有这些代码都放在函数之外,您将在读取文件时强制它 运行,这在任何不是简单测试或玩具的代码中都是次优的。
其他说明:
您在两个不同的位置定义了两次 InputNumber
,而第一个定义仅在 StartQuestion == 'a'
时发生。不要这样做。有条件地定义 variables/functions 将使您的代码容易出现 NameError
s,因为您允许使用未定义的函数的可能性。只需在顶部定义一次即可。
Variable names should be in lower_snake_case. UpperCamelCase 保留用于 class 个名称。
一旦此代码 有效并完成 ,您可以 post 在 Code Review 上完成它,人们可以提出进一步的建议.
抱歉,我是 python 的新手,我知道这可能很容易解决,但我已经被困了好久。
这是我的代码:
CountFromNum = InputNumber("Which number would you like to count down from? ")
print("This is Joe's Multiple Program Loader, hope you enjoy.")
print("Would you like to:")
print("(a) Count down from a certain number.")
print("(b) Print your name a certain amount of times.")
StartQuestion = input("(c) make a list with a custom length. ")
StartQuestionOptions='abc'
while StartQuestion not in StartQuestionOptions :
StartQuestion=input("Please enter the letter a, b or c: ")
if StartQuestion in StartQuestionOptions :
break
else :
StartQuestion=input("Please enter the letter a, b or c: ")
#Module A: P1
if StartQuestion == 'a':
print("You have selected: Option a")
def InputNumber(message):
while True:
try:
Userinput = int(input(message))
except ValueError:
print("Not a number! Please try again.")
continue
else:
return Userinput
break
CountFromNum = InputNumber("Which number would you like to count down from? ")
#Module B: P1
if StartQuestion == 'b':
print("You have selected: Option b")
Username = input("Please enter your name: ")
# Module B: P2
def InputNumber(message):
while True:
try:
Userinput = int(input(message))
except ValueError:
print("Please enter a number, try again.")
continue
else:
return Userinput
break
#Module B: P3
NameRepeatValue = inputNumber("How many times do you want to repeat your name? ")
while NameRepeatValue > 0:
print (Username)
NameRepeatValue -=1
while NameRepeatValue > 0:
exit()
#Module C: P1
if StartQuestion == 'c':
print("You have selected: Option c")
if StartQuestion == 'abc':
print("Stop trying to break my program.")
每当我做某事时总是出错,请帮忙。
主要问题是您试图在函数定义之前使用它。将 InputNumber
的定义移至文件顶部;或至少在使用之前。
您应该开始练习的另一个修复方法是将所有代码包装在函数中。您在顶层拥有的所有不是函数定义的代码最好都应该放入 main
函数或类似的东西中。这将在这里解决您的问题,并且如果您开始使用 REPL 进行开发,还可以简化以后的开发。通过将所有这些代码都放在函数之外,您将在读取文件时强制它 运行,这在任何不是简单测试或玩具的代码中都是次优的。
其他说明:
您在两个不同的位置定义了两次
InputNumber
,而第一个定义仅在StartQuestion == 'a'
时发生。不要这样做。有条件地定义 variables/functions 将使您的代码容易出现NameError
s,因为您允许使用未定义的函数的可能性。只需在顶部定义一次即可。Variable names should be in lower_snake_case. UpperCamelCase 保留用于 class 个名称。
一旦此代码 有效并完成 ,您可以 post 在 Code Review 上完成它,人们可以提出进一步的建议.