O'Reilly logo

Python For Kids For Dummies by Brendan Scott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

PROJECT 3

Guessing Game

Guess what? You’re going to create a game where the computer chooses a number and the player guesses the number.

image

This project teaches you how to get input from the user and how numbers work in Python (they’re different from strings). You create a random number and in the process reuse someone else’s code in your own projects. You also do a little bit of debugging and tracking down logical errors.

Plan the Game

In the guessing game, Python thinks of a random number. The player guesses the number.

  • If the player guesses correctly, Python says so.
  • If the player guesses incorrectly, Python says whether the number is higher or lower.
  • The player keeps guessing until the player gets the right number.

If you did Project 2, you already know how to tell the player something — that’s what print is for. However, what you don’t know about the game at the moment is how to get Python to think of a random number, or how to get a guess from the player.

Get Input from the Player

In practice, your program will always need some kind of input from the user. You’re still using the command line right now, so text is the only way to get input. You get text from a user by ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required