[Tutor] Tutor Digest, Vol 169, Issue 20

David Brown david.brown.rrt at gmail.com
Mon Mar 26 16:44:42 EDT 2018


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, <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. Problems with Looping (David Brown)
>    2. Re: Problems with Looping (Joel Goldstick)
>
>
> ---------- Forwarded message ----------
> From: David Brown <david.brown.rrt at gmail.com>
> To: tutor at 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 <joel.goldstick at gmail.com>
> To:
> Cc: tutor at 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 <david.brown.rrt at gmail.com>
> 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 at 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 at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>


-- 
*David*

David Brown, BS RRT


More information about the Tutor mailing list