Home > Back-end >  Reference generator. How can I collect users input into a list?
Reference generator. How can I collect users input into a list?

Time:10-01

I'm new to Python and I have started my first project. I want to make a reference generator. However, I have faced a problem with authors name. Right now my code can accept and obtain only one author (name and surname), but I want it to be able to collect a few authors. I am struggling to find a logic that will help me to collect few authors names and save them into a list.

I will try to explain it with an example:

authors_name = input('Enter authors name: ')
question = input('Add another author? [Yes | No]: ')

while question.lower() = 'yes':
new_author = input('Enter authors name: ')

This is the part where I'm stuck. How can I make my program to remember the first entered name and the names that will come after that? I suppose I should use lists or smthng.

Thanks in advance

CodePudding user response:

Try something like this:

list_of_authors = []
question = lambda: input('Add another author? [Yes | No]: ').lower()

while True:
    author_name = input("Enter author name: ")
    list_of_authors  = [author_name]
    if question() == "no":
        break

CodePudding user response:

It's unfriendly to require people to answer an extra question. Just keep asking for more names until they press "enter".

print( "Press enter to stop entering names." )
authors = []
while True:
    authors_name = input('Enter authors name: ')
    if not authors_name:
        break
    authors.append( authors_name )

print( "The list I got was", authors )
  • Related