[Tutor] Problems with Looping

2018-03-26 Thread David Brown
Hello all,

I am trying to teach myself Python by working through a book on Safari
Books. One of the examples in the chapter on Loops states:

*"Write a program that simulates a fortune cookie. The program should
display one of five unique fortunes, at random, each time it’s run."*

The program below does that just fine. However, I wanted to take it to the
next level and allow the user to select another fortune if desired. I've
tried a variety of "While" loops, but they end up with infinite loops, or
not generating a fortune. I've tried to add continue, after each "if, elif"
statement, but I usually get an error stating that it's not in the correct
place in the loop.

Can any one give me a hint? I'd like to figure it out on my own, but am
stuck.

This is NOT for a graded assignment. It is purely for self study.

# Fortune Cookie
# Displays Random Fortune

import random

# Generate one of five fortunes randomly

print("\t\tCyber Fortune Cookie")
input(" \n\tHit ENTER to see your fortune")

# Set the initial values
prefix = "Your fortune is: "
number = random.randrange(5) + 1

if number == 1:
print("\n", prefix,"When much wine goes in very bad things come out.")

elif number == 2:
print("\n", prefix,"You will be hungry in one hour.")

elif number == 3:
print("\n", prefix,"Order another egg roll and you will be happy.")

elif number == 4:
print("\n", prefix,"You will love the spicy shrimp and garlic.")

elif number == 5:
print("\n", prefix,"You will meet an old friend soon.")

#input("\n\nPress the enter key to get another fortune.")
-- 
*David*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problems with Looping

2018-03-26 Thread Joel Goldstick
On Mon, Mar 26, 2018 at 9:45 AM, David Brown  wrote:
> Hello all,
>
> I am trying to teach myself Python by working through a book on Safari
> Books. One of the examples in the chapter on Loops states:
>
> *"Write a program that simulates a fortune cookie. The program should
> display one of five unique fortunes, at random, each time it’s run."*
>
> The program below does that just fine. However, I wanted to take it to the
> next level and allow the user to select another fortune if desired. I've
> tried a variety of "While" loops, but they end up with infinite loops, or
> not generating a fortune. I've tried to add continue, after each "if, elif"
> statement, but I usually get an error stating that it's not in the correct
> place in the loop.
>
> Can any one give me a hint? I'd like to figure it out on my own, but am
> stuck.
>
> This is NOT for a graded assignment. It is purely for self study.
>
> # Fortune Cookie
> # Displays Random Fortune
>
> import random
>
> # Generate one of five fortunes randomly
>
> print("\t\tCyber Fortune Cookie")
> input(" \n\tHit ENTER to see your fortune")
>
> # Set the initial values
> prefix = "Your fortune is: "
> number = random.randrange(5) + 1
>
> if number == 1:
> print("\n", prefix,"When much wine goes in very bad things come out.")
>
> elif number == 2:
> print("\n", prefix,"You will be hungry in one hour.")
>
> elif number == 3:
> print("\n", prefix,"Order another egg roll and you will be happy.")
>
> elif number == 4:
> print("\n", prefix,"You will love the spicy shrimp and garlic.")
>
> elif number == 5:
> print("\n", prefix,"You will meet an old friend soon.")
>
> #input("\n\nPress the enter key to get another fortune.")
> --
> *David*
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


set a variable, say 'another_fortune' to True.
surround your code in a while loop, checking the value of another_fortune

At the end of your code, ask if the user wants another, and check
input for 'y' or 'Y', and if you don't get that, set another_fortune =
False
-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 169, Issue 20

2018-03-26 Thread David Brown
Thanks Joel. That got me going in the right direction. I ended up using a
"while true" statement. The code below seems to work now. It will accept
any key and not just "Y". Haven't gotten that far with it yet.

# Fortune Cookie
# Displays Random Fortune
# Allows user to see repeated fortunes up to 5

import random

# Generate one of five fortunes randomly

print("\t\t~~Cyber Fortune Cookie~~")

# Set the initial values
prefix = "Your fortune is: "
count = 0
while True:
input("  \n\tHit ENTER to see your fortune")
number = random.randint (1,6)
if number == 1:
print("\n", prefix,"When much wine goes in very bad things come
out.")
count += 1
elif number == 2:
print("\n", prefix,"You will be hungry in one hour.")
count += 1
elif number == 3:
print("\n", prefix,"Order another egg roll and you will be happy.")
count += 1
elif number == 4:
print("\n", prefix,"You will love the spicy ginger shrimp.")
count += 1
elif number == 5:
print("\n", prefix,"You will meet an old friend soon.")
count += 1
if count > 5:
print("\nNo more fortunes for you")
break
if count <5:
input("\n\nEnter 'Y' if you would like another fortune: ")
continue


On Mon, Mar 26, 2018 at 12:00 PM,  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. Problems with Looping (David Brown)
>2. Re: Problems with Looping (Joel Goldstick)
>
>
> -- Forwarded message --
> From: David Brown 
> To: tutor@python.org
> Cc:
> Bcc:
> Date: Mon, 26 Mar 2018 09:45:19 -0400
> Subject: [Tutor] Problems with Looping
> Hello all,
>
> I am trying to teach myself Python by working through a book on Safari
> Books. One of the examples in the chapter on Loops states:
>
> *"Write a program that simulates a fortune cookie. The program should
> display one of five unique fortunes, at random, each time it’s run."*
>
> The program below does that just fine. However, I wanted to take it to the
> next level and allow the user to select another fortune if desired. I've
> tried a variety of "While" loops, but they end up with infinite loops, or
> not generating a fortune. I've tried to add continue, after each "if, elif"
> statement, but I usually get an error stating that it's not in the correct
> place in the loop.
>
> Can any one give me a hint? I'd like to figure it out on my own, but am
> stuck.
>
> This is NOT for a graded assignment. It is purely for self study.
>
> # Fortune Cookie
> # Displays Random Fortune
>
> import random
>
> # Generate one of five fortunes randomly
>
> print("\t\tCyber Fortune Cookie")
> input(" \n\tHit ENTER to see your fortune")
>
> # Set the initial values
> prefix = "Your fortune is: "
> number = random.randrange(5) + 1
>
> if number == 1:
> print("\n", prefix,"When much wine goes in very bad things come out.")
>
> elif number == 2:
> print("\n", prefix,"You will be hungry in one hour.")
>
> elif number == 3:
> print("\n", prefix,"Order another egg roll and you will be happy.")
>
> elif number == 4:
> print("\n", prefix,"You will love the spicy shrimp and garlic.")
>
> elif number == 5:
> print("\n", prefix,"You will meet an old friend soon.")
>
> #input("\n\nPress the enter key to get another fortune.")
> --
> *David*
>
>
>
> -- Forwarded message --
> From: Joel Goldstick 
> To:
> Cc: tutor@python.org
> Bcc:
> Date: Mon, 26 Mar 2018 09:56:30 -0400
> Subject: Re: [Tutor] Problems with Looping
> On Mon, Mar 26, 2018 at 9:45 AM, David Brown 
> wrote:
> > Hello all,
> >
> > I am trying to teach myself Python by working through a book on Safari
> > Books. One of the examples in the chapter on Loops states:
> >
> > *"Write a program that simulates a fortune cookie. The program should
> > display one of five unique fortunes, at random, each time it’s run."*
> >
> > The program below does that just fine. However, I wanted to take it to
> the
> > next level and allow the user to select another fortune if desired. I've
> > tried a variety of "While" loops, but they end up with infinite loops, or
> > not generating a fortune. I've tried to add continue, after each "if,
> elif"
> > statement, but I usually get an error stating that it's not in the
> correct
> > place in the loop.
> >
> > Can any one give me a hint? I'd like to figure it out on my own, but am
> > stuck.
> >
> > This is NOT for a graded assignment. It is purely for self study.
> >
> > # Fortune Cookie
> > # Displays Random Fortune
> >

Re: [Tutor] Tutor Digest, Vol 169, Issue 20

2018-03-26 Thread Alan Gauld via Tutor
First of all, please do not repost the entire digest - we have
all already seen them -  and some people pay by the byte.

Secondly...

On 26/03/18 21:44, David Brown wrote:
> Thanks Joel. That got me going in the right direction. I ended up using a
> "while true" statement. The code below seems to work now. It will accept
> any key and not just "Y". Haven't gotten that far with it yet.

How do you exit the loop?

Your code as it stands just ignore the users input completely.
You may as well miss out the line.

That's why Joel suggested a check, so that the user can quit
nicely.

> while True:
> input("  \n\tHit ENTER to see your fortune")
> number = random.randint (1,6)
> if number == 1:
> print("\n", prefix,"When much wine goes in very bad things come
> out.")
> count += 1
> elif number == 2:

> if count > 5:
> print("\nNo more fortunes for you")
> break
> if count <5:

So you have to do it 5 times, even if you don't want to?


> input("\n\nEnter 'Y' if you would like another fortune: ")
> continue

This ignores the user's response.

If you really want a while True loop then might I suggest
restructuring to something like:

while True:
if input("Had enough yet? ").lowercase[0] == 'y':
   break # exit the loop
# continue with the original if/elif tree here


-- 
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