the Guess a Number Problem in Python

1. Guess a Number

Games that involve guessing are common, and are sometimes used to resolve minor conflicts, such as who gets the next piece of cake or who gets the first kick at a football. It’s also sometimes a way to occupy time, and can simply be fun. How can we write a program to have the user guess a number that the program has chosen?

There are many variations on this simple game. In one version, the number is to be guessed precisely. One person (the chooser) selects a number, an integer, in a specified range. “Pick a number between one and ten” is a typical expression of this kind of problem. The other person, the guesser, must choose a number in that range. If the guesser selects the correct number, then the guesser wins. This is a boring game and is biased in favor of the chooser.

A more interesting variation is to start with one guess and have the chooser then say whether the target number is greater than or less than the guessed num­ber. The guesser then guesses again, and the process continues until the number is guessed correctly. The roles of guesser and chooser can now switch and the game starts again. The best guesser is the one who uses the fewest guesses.

A third alternative is to have multiple guessers. All guessers make their selection and the one who has chosen a number nearest the correct number is the winner. This is the best game for solving disputes, because it involves one guess from each person. Ties are possible, in which case the game can be played again.

2. Solving the Guess a Number Problem

The simple version of the guessing program has two versions depending on who is guessing. The computer should pick the number and the human user should guess, because the other way around involves some complex program­ming. Here’s what has to happen for this game to be successful:

  1. The computer selects a number.
  2. The computer asks the player to guess.
  3. The player types a number on the keyboard and the computer reads it in.
  4. The computer compares the input number against the one that it selected and if the two agree, then the player wins. Otherwise, the computer wins.

The Python features needed to do this include printing a message, reading in a number, having a place to store a number (a variable), having a way to select a number, and having a way to compare the two numbers and act differently de­pending on the result.

The second version requires the above, plus a way to repeat the process in cases when the guess is wrong and until it is correct. In this case the method becomes:

  1. The computer selects a number.
  2. The computer asks the player to guess.
  3. The player types a number on the keyboard and the computer reads it in.
  4. The computer compares the input number against the one that it selected and if the two agree, then the player has guessed correctly. Exit to Step 7.
  5. The computer determines whether the guess is higher or lower than the actual number and prints an appropriate message.
  6. Repeat from Step 2.
  7. Game over.

The repetition mechanism is the only new aspect to this solution, but is an essential component of Python and every other programming language.

 

Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.

Leave a Reply

Your email address will not be published. Required fields are marked *