Tradeback 错误和 NameError python
Tradeback error and NameError python
我正在尝试输入一个介于 1-9 之间的值,然后打印用户输入的数字的 word 版本。我不断收到回溯错误和名称错误:
Traceback (most recent call last):
File "/Users/Michell/Desktop/testq1.1.py", line 13, in <module>
main()
File "/Users/Michell/Desktop/testq1.1.py", line 11, in main
print (fun(num))
File "/Users/Michell/Desktop/testq1.1.py", line 3, in fun
word2num = numlist[num-1]
NameError: name 'numlist' is not defined*
我的代码如下:
def fun(num):
word2num = numlist[num-1]
return numlist
return num
def main():
num = eval(input("Enter a # between 1-9: "))
numlist = ["one","two","three","four","five","six","seven","eight","nine"]
print (fun(num))
main()
您需要将 numlist
传递给您的 fun
函数和 return word2num
而不是整个列表:
def fun(num, numlist):
word2num = numlist[num - 1]
return word2num
def main():
num = int(input("Enter a # between 1-9: "))
numlist = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
print(fun(num, numlist))
main()
我正在尝试输入一个介于 1-9 之间的值,然后打印用户输入的数字的 word 版本。我不断收到回溯错误和名称错误:
Traceback (most recent call last):
File "/Users/Michell/Desktop/testq1.1.py", line 13, in <module>
main()
File "/Users/Michell/Desktop/testq1.1.py", line 11, in main
print (fun(num))
File "/Users/Michell/Desktop/testq1.1.py", line 3, in fun
word2num = numlist[num-1]
NameError: name 'numlist' is not defined*
我的代码如下:
def fun(num):
word2num = numlist[num-1]
return numlist
return num
def main():
num = eval(input("Enter a # between 1-9: "))
numlist = ["one","two","three","four","five","six","seven","eight","nine"]
print (fun(num))
main()
您需要将 numlist
传递给您的 fun
函数和 return word2num
而不是整个列表:
def fun(num, numlist):
word2num = numlist[num - 1]
return word2num
def main():
num = int(input("Enter a # between 1-9: "))
numlist = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
print(fun(num, numlist))
main()