Reputation: 229
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()
Upvotes: 1
Views: 160
Reputation: 2786
I modified your program a little bit:
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.lower() in ['n','no']:
print 'Here is the list of words:', x
return x
else:
x.append(response)
def main():
returned_list = add_list(x)
print "I can also print my returned list: ", returned_list
x = []
main()
First: There is no need for recursion. Instead simply append response
to your list x
. A nice feature of python is to check if response
is not just n
, but anything close to n
using:
if response.lower() in ['n','no']:
I also edited a line that actually prints the list to your user!
print 'Here is the list of words:', x
Finally, you can also print your list after you return it. Check out the edited def main():
.
Upvotes: 0
Reputation: 37249
As mentioned above, the reason your code does not return anything is because you don't actually print your results. Furthermore, the reason it continues to ask you if you'd like to enter another word even after you've already said 'n' is due to recursion (i.e. the fact you are calling the function over and over in a nested manner (think nested IFs in Excel, if that helps :) ). Once you get a grasp of the basics you can read up on it more, but for now I would avoid it :)
Here is a basic version of something that will do what you want, with comments that will hopefully help you understand what is going on:
def add_list():
# Since you are starting with an empty list and immediately appending
# the response, you can actually cut out the middle man and create
# the list with the result of the response all at once (notice how
# it is wrapped in brackets - this just says 'make a list from the
# result of raw_input'
x = [raw_input('Please input a word to add to a list: ')]
# This is a slightly more idiomatic way of looping in this case. We
# are basically saying 'Keep this going until we break out of it in
# subsequent code (you can also say while 1 - same thing).
while True:
response = raw_input('Would you like to enter another word? ')
# Here we lowercase the response and take the first letter - if
# it is 'n', we return the value of our list; if not, we continue
if response.lower()[0] == 'n':
return x
else:
# This is the same concept as above - since we know that we
# want to continue, we append the result of raw_input to x.
# This will then continue the while loop, and we will be asked
# if we want to enter another word.
x.append(raw_input('Please input a word to add to the list: '))
def main():
# Here we return the result of the function and save it to my_list
my_list = add_list()
# Now you can do whatever you want with the list
print 'Here is the list of words: ', my_list
main()
Upvotes: 2