Guess the Number

Back in the early days of this blog, I posted a screenshot of a program that plays “guess the number”, but I thought I’d like to do a proper post on it,

This program is very simple, so I won’t write too much about it. The only thing here that might be new is the “try:” section. This is a bit of error handling. If your user types in a number above 100 or below 0, the program can cope very well. But if the user types in something that is not a (whole) number, there would be an error if we didn’t use this approach. Basically, the program tries to convert “guess” to an integer. If this isn’t possible, the “ValueError” is trapped and the code tells the user that he/she didn’t enter a number.

Anyway, have a go. “Guess the Number” is a classic beginner’s program!

antiloquax

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: