Working with Python List

Good day everyone! Hope all is well.

I am trying to create an empty list, eg: week_days. then set up a loop for the user to enter each of the days of the week one at a time by using the append method, (Sunday, Monday, Tuesday, etc……………) and add the days to the list week_days as they are entered by the user.

This is what I have, not sure it’s the right way.

weekdays = []

days = raw_input("Enter week days: ")
weekdays.append(days)

for days in weekdays:
       print days

Thanks everyone reading this.

IC

You are only reading in one day and saving that to the list before looping through the list to output it.

You need to build the raw_input and append statements into a second loop where you keep requesting days and appending them to the list until some specific termination condifion is met (eg. not entering anything).

Additionally, you could comma separate the dates and use the split function built into Python.

If you’re just trying to learn the language and how for looping works, sick with Felgall’s recommendation.

Also, http://docs.python.org/tutorial/ is really all you need to learn the language.

As fegall said, you created a loop to output the results, but you didn’t create a loop to gather the results. Here is an example of what you can do:


entered_data = []

while (1):  #create an infinite loop
    entered = raw_input("Type stuff ('q' to quit): ")

    if entered in ['Q', 'q', 'Quit', 'quit']:
        break  #break out of the loop

    entered_data.append(entered)

for entered in entered_data:
    print entered