I'm trying to enter a value between 1-9 and then print the word version of the number the user entered. I keep getting a traceback error and name error:
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*
My code is below:
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()
You need to pass numlist
to your fun
function and return the word2num
rather than the whole list:
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()