-->

A Short Program: Guess the Number [Python]


The toy examples I’ve show you so far are useful for introducing basic concepts, but now let’s see how everything you’ve learned comes together in a more complete program. In this section, I’ll show you a simple “guess the number” game. When you run this program, the output will look something like this:

  1. I am thinking of a number between 1 and 20.
  2. Take a guess.
  3. 10
  4. Your guess is too low.
  5. Take a guess.
  6. 15
  7. Your guess is too low.
  8. Take a guess.
  9. 17
  10. Your guess is too high.
  11. Take a guess.
  12. 16
  13. Good job! You guessed my number in 4 guesses!

Type the following source code into the file editor, and save the file as guessTheNumber.py:

  1. # This is a guess the number game.
  2. import random
  3. secretNumber = random.randint(1, 20)
  4. print('I am thinking of a number between 1 and 20.')
  5. # Ask the player to guess 6 times.
  6. for guessesTaken in range(1, 7):
  7. print('Take a guess.')
  8. guess = int(input())
  9. if guess < secretNumber:
  10. print('Your guess is too low.')
  11. elif guess > secretNumber:
  12. print('Your guess is too high.')
  13. else:
  14. break # This condition is the correct guess!
  15. if guess == secretNumber:
  16. print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
  17. else:
  18. print('Nope. The number I was thinking of was ' + str(secretNumber))

Let’s look at this code line by line, starting at the top.

  1. # This is a guess the number game.
  2. import random
  3. secretNumber = random.randint(1, 20)

First, a comment at the top of the code explains what the program does. Then, the program imports the random module so that it can use the random.randint() function to generate a number for the user to guess. The return value, a random integer between 1 and 20, is stored in the variable secretNumber.

  1. print('I am thinking of a number between 1 and 20.')
  2. # Ask the player to guess 6 times.
  3. for guessesTaken in range(1, 7):
  4. print('Take a guess.')
  5. guess = int(input())

The program tells the player that it has come up with a secret number and will give the player six chances to guess it. The code that lets the player enter a guess and checks that guess is in a for loop that will loop at most six times. The first thing that happens in the loop is that the player types in a guess. Since input() returns a string, its return value is passed straight into int(), which translates the string into an integer value. This gets stored in a variable named guess.

  1. if guess < secretNumber:
  2. print('Your guess is too low.')
  3. elif guess > secretNumber:
  4. print('Your guess is too high.')

These few lines of code check to see whether the guess is less than or greater than the secret number. In either case, a hint is printed to the screen.

  1. else:
  2. break # This condition is the correct guess!

If the guess is neither higher nor lower than the secret number, then it must be equal to the secret number, in which case you want the program execution to break out of the for loop.

  1. if guess == secretNumber:
  2. print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
  3. else:
  4. print('Nope. The number I was thinking of was ' + str(secretNumber))

After the for loop, the previous if...else statement checks whether the player has correctly guessed the number and prints an appropriate message to the screen. In both cases, the program displays a variable that contains an integer value (guessesTaken and secretNumber). Since it must concatenate these integer values to strings, it passes these variables to the str() function, which returns the string value form of these integers. Now these strings can be concatenated with the + operators before finally being passed to the print() function call.

Summary

Functions are the primary way to compartmentalize your code into logical groups. Since the variables in functions exist in their own local scopes, the code in one function cannot directly affect the values of variables in other functions. This limits what code could be changing the values of your variables, which can be helpful when it comes to debugging your code.

Functions are a great tool to help you organize your code. You can think of them as black boxes: They have inputs in the form of parameters and outputs in the form of return values, and the code in them doesn’t affect variables in other functions.

In previous article, a single error could cause your programs to crash. In this article, you learned about try and except statements, which can run code when an error has been detected. This can make your programs more resilient to common error cases.



0 Response to "A Short Program: Guess the Number [Python]"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel