The numberguessing game again

In chapter 1, we made a simple number-guessing program. Now let's try the same thing, but using EasyGui for the input and output. Listing 6.5 has the code.

Listing 6.5 Number-guessing game using EasyGui import random, easygui secret = random.randint(1, 99) guess = 0 tries = 0

Picks a secret number easygui.msgbox( AHOY! I'm the Dread Pirate Roberts, and I have a secret!

It is a number from 1 to 99. I'll give you 6 tries )

Gets the players guess while guess != secret and tries < 6:

guess = easygui.integerbox("What's yer guess, matey?") if not guess: break if guess < secret:

easygui.msgbox(str(guess) + " is too low, ye scurvy dog!") elif guess > secret:

easygui.msgbox(str(guess) + " is too high, landlubber!") tries = tries + 1

Uses up one try if guess == secret:

easygui.msgbox("Avast! Ye got it! Found my secret, ye did!") else:

easygui.msgbox("No more guesses! Better luck next time, matey!

Allows up to 6 guesses

Prints message at end of game

We still haven't learned how all the parts of this program work, but type it in and give it a try. You should see something like this when you run it:

Easygui Integerbox

We'll be learning about if, else, and elif in chapter 7, and while in chapter 8. We'll learn about random in chapter 15, and we'll use it a lot more in chapter 23.

100 Bowling Tips

100 Bowling Tips

Playing bowling with your friends can help you decide if it is indeed the hobby that you want to invest your time on today. Aside from that, it can help you get a better feel of the sport. More importantly, when you play with your friends, it would become a more fun activity, which you can look forward to each week.

Get My Free Ebook


Responses

  • Prudenzia
    How to programming python numbers 199?
    7 years ago

Post a comment