Python Program to Create Number Guessing Game
random.randint() to pick a secret number and a while loop to let the user guess until they find it, like this: import random; secret = random.randint(1, 100); guess = None; while guess != secret: guess = int(input('Guess: ')); if guess < secret: print('Too low'); elif guess > secret: print('Too high'); else: print('You guessed it!').Examples
How to Think About It
Algorithm
Code
import random secret_number = random.randint(1, 100) guess = None while guess != secret_number: guess = int(input('Guess a number between 1 and 100: ')) if guess < secret_number: print('Too low, try again.') elif guess > secret_number: print('Too high, try again.') else: print('Congratulations! You guessed the number.')
Dry Run
Let's trace guessing 63 when the secret number is 63.
Generate secret number
secret_number = 63
First guess
guess = 50; guess < secret_number → print 'Too low, try again.'
Second guess
guess = 75; guess > secret_number → print 'Too high, try again.'
Third guess
guess = 63; guess == secret_number → print 'Congratulations! You guessed the number.'; exit loop
| Guess | Comparison | Output |
|---|---|---|
| 50 | 50 < 63 | Too low, try again. |
| 75 | 75 > 63 | Too high, try again. |
| 63 | 63 == 63 | Congratulations! You guessed the number. |
Why This Works
Step 1: Pick a secret number
The program uses random.randint(1, 100) to choose a secret number between 1 and 100.
Step 2: Get user guesses
It asks the player to enter guesses using input() and converts them to numbers with int().
Step 3: Compare and respond
The program compares each guess to the secret number and tells the player if the guess is too low, too high, or correct.
Step 4: Repeat until correct
The while loop keeps asking for guesses until the player finds the secret number, then congratulates them.
Alternative Approaches
import random secret_number = random.randint(1, 100) max_attempts = 5 attempts = 0 while attempts < max_attempts: guess = int(input('Guess a number between 1 and 100: ')) attempts += 1 if guess < secret_number: print('Too low, try again.') elif guess > secret_number: print('Too high, try again.') else: print('You guessed it!') break else: print(f'Sorry, you ran out of guesses. The number was {secret_number}.')
import random def guessing_game(): secret = random.randint(1, 100) while True: guess = int(input('Your guess: ')) if guess < secret: print('Too low') elif guess > secret: print('Too high') else: print('Correct!') break guessing_game()
Complexity: O(k) time, O(1) space
Time Complexity
The program runs in O(k) time where k is the number of guesses the user makes until they find the secret number. Each guess is processed once.
Space Complexity
The program uses O(1) space because it stores only a few variables regardless of input size.
Which Approach is Fastest?
All approaches have similar time and space complexity; differences lie in user experience like guess limits or code structure.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic loop | O(k) | O(1) | Simple, interactive guessing |
| Limited guesses | O(m) | O(1) | Games with challenge and guess limits |
| Function-based | O(k) | O(1) | Reusable and organized code |
int() before comparing numbers.