[Tutor] Tutor Digest, Vol 157, Issue 39

Marc Sebek marc.sebek at gmail.com
Sun Mar 19 15:20:42 EDT 2017


Hi Rafael

You are appending quit to the list, before checking to see if quit has been
typed. You could move the "if Statement" up in the code and add an else
statement, so Quit is not appended to the list first thing.

while True:
    activity = input(prompt)

    if activity == "quit":
        print("Let me recap. This is what you've done today: %s." % ",
" .join(LogActivities))
   else:
        LogActivities.append(activity)

But you have an infinite loop here too, so unless you are adding more to
this, and plan to get out the loop, you should also add a break statment:
++++++++++++++++++++++++++++++++++++
    if activity == "quit":
        print("Let me recap. This is what you've done today: %s." % ",
" .join(LogActivities))
   break ###<<<<Add a break
+++++++++++++++++++++++++++++++++++

Or change the code structure and loop condition:

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

activity = input(prompt)

while activity != "quit":
    LogActivities.append(activity)
    activity = input(prompt)

print("Let me recap. This is what you've done today: %s." %
",".join(LogActivities))
++++++++++++++++++++++++++++++++++++++

There's probably a more pythonistic way to write this, but without changing
the current set of statements too much, this works.

Cheers

On Mon, Mar 20, 2017 at 5:00 AM, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
> Today's Topics:
>
>    1. tiny, little issue with list (Rafael Knuth)
>
>
> ---------- Forwarded message ----------
> From: Rafael Knuth <rafael.knuth at gmail.com>
> To: "Tutor at python.org" <Tutor at python.org>
> Cc:
> Bcc:
> Date: Sun, 19 Mar 2017 13:17:03 +0100
> Subject: [Tutor] tiny, little issue with list
> 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
> https://mail.python.org/mailman/listinfo/tutor
>
>


More information about the Tutor mailing list