[Tutor] tiny, little issue with list

2017-03-19 Thread Rafael Knuth
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tiny, little issue with list

2017-03-19 Thread Alan Gauld via Tutor
On 19/03/17 12:17, Rafael Knuth wrote:
> LogActivities = []
> prompt = ("What have you done today? ")
> prompt += ("Enter 'quit' to exit. ")

I'm not sure why you put that on two lines but
thats just a nit pick...

> while True:

This will loop forever unless you explicitly break,
return or hit an exception. You should fix that.

> activity = input(prompt)
> LogActivities.append(activity)

You append activity regardless of what it is so
obviously you include the 'quit'.

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

Then after appending it you test to see if its
quit then print a message(but don;t actually
quit, see above)


To avoid the append you could put the test for
quit at the top of the loop and put the append
lines in an else:

if activity == 'quit':
   print # and break?
else:
   append


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 157, Issue 39

2017-03-19 Thread Marc Sebek
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 ###<<< wrote:

> Send Tutor mailing list submissions to
> tutor@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-requ...@python.org
>
> You can reach the person managing the list at
> tutor-ow...@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 
> To: "Tutor@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@python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor