SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    weekdays = []
    
    days = raw_input("Enter week days: ")
    weekdays.append(days)
    
    for days in weekdays:
           print days
    Thanks everyone reading this.


    IC

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code Python:
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •