I'm having trouble creating a function in Python. I'd like my function to allow a user to input words in a list and ask the user if they'd like to input additional words. If the user inputs 'n', I'd like the function to return the words entered in the list. When I run the function, it asks the user if they'd like to enter an additional word twice. Also, it doesn't return the list.
def add_list(x):
first_list = raw_input('Please input a word to add to a list ')
x.append(first_list)
response = None
while response != 'n':
response = raw_input('Would you like to enter another word? ')
if response == 'n':
print 'Here is the list of words'
return x
else:
add_list(x)
def main():
add_list(x)
x = []
main()
add_listfrom insideadd_list, and you may have an easier time of it.elsestatement toreturn add_list(x). You want all decision paths to return the list.return xreturns the list, but nothing is done with it. If you putprintin front ofadd_list(x)inmain(), you should see your list if you press 'n' during the first run.