[Tutor] tiny, little issue with list

Sri Kavi gvmcmt at gmail.com
Mon Mar 20 05:17:49 EDT 2017


Hi Rafael,


LogActivities = []

prompt = ("What have you done today? Enter 'quit' to exit. ")

while True:
    activity = input(prompt)

    # Do this here so 'quit' is not appended to the list
    if activity == 'quit':  # To get out of the loop
        break

    # If still in the loop, append this activity to the list
    # Note, no else statement here
    LogActivities.append(activity)

# Do this outside the loop
if LogActivities:   # See if the list is not empty
    print("Let me recap. This is what you've done today: %s." %
    ", " .join(LogActivities))


Sri

On Sun, Mar 19, 2017 at 5:47 PM, Rafael Knuth <rafael.knuth at gmail.com>
wrote:

> LogActivities = []
> prompt = ("What have you done today? ")
> prompt += ("Enter 'quit' to exit. ")
>
> while True:
>     activity = input(prompt)
>     LogActivities.append(activity)
>
>     if activity == "quit":
>         print("Let me recap. This is what you've done today: %s." % ",
> " .join(LogActivities))
>
> This program is supposed to take user input and to log his activities
> into a list.
> All works well, only when the user quits, the program adds 'quit' to
> LogActivities.
> How do I prevent my program from doing this?
>
> Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900
> 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
> >>>
> = RESTART: C:/Users/Rafael/Documents/01 - BIZ/PYTHON/Python
> Code/PPC_159.py =
> What have you done today? Enter 'quit' to exit. shower
> What have you done today? Enter 'quit' to exit. walk the dog
> What have you done today? Enter 'quit' to exit. drink coffee
> What have you done today? Enter 'quit' to exit. prepare lunch
> What have you done today? Enter 'quit' to exit. take coding lesson
> What have you done today? Enter 'quit' to exit. quit
> Let me recap. This is what you've done today: shower, walk the dog,
> drink coffee, prepare lunch, take coding lesson, quit.
> What have you done today? Enter 'quit' to exit.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list