PYTHON PROGRAMMING
Number Guessing Game — Progressive Levels
Level 1 — Basic Guess Check
Print whether the user guessed the number right or wrong.
Code:
secret_code == 56
user_input = int(input("enter the guess:"))
if secret_code == user_input:
print("congrualations you are guess is right:")
else:
print("sorry your guess is wrong")
Output:
enter the guess: 56
congrualations you are guess is right:
Level 2 — 7 Trials with For Loop
Print if the user guessed right or wrong within 7 trials. Introduces the for loop.
Code:
secret_code = 56
for i in range(0, 7):
user_input = int(input("enter the guess:"))
if secret_code == user_input:
print("congrualations you are guess is right:")
else:
print("sorry your guess is wrong")
break
Output:
enter the guess: 56
congrualations you are guess is right:
enter the guess: 55
sorry your guess is wrong
Level 2.2 — Infinite Trials with While Loop
When the number of trials is not defined — runs infinitely. Introduces the while loop.
Code:
secret_code = 56
while True:
user_input = int(input("enter the guess:"))
if secret_code == user_input:
print("congrualations you are guess is right:")
break
else:
print("sorry your guess is wrong")
Level 3 — Multiple Conditions with elif
Introduces nested elif conditions:
1. User must enter a number between 1–100. Otherwise: "invalid Number"
2. If guess > secret: "YOUR NUMBER IS GREATER THAN SECRET NUMBER"
3. If guess < secret: "YOUR NUMBER IS LESSER THAN SECRET NUMBER"
Code:
secret_code = 88
for i in range(0, 7):
user_input = int(input("enter the guess:"))
if user_input < 1 or user_input > 100:
print("invalid number: please enter number 1=100")
elif user_input > secret_code:
print("your number is higher")
elif user_input < secret_code:
print("your number is lesser")
else:
print("your number is correct")
print(f"congradulations,you have guessed the number {secret_code} in {i}
attempts")
break
Output:
enter the guess: 55 → your number is lesser
enter the guess: 58 → your number is lesser
enter the guess: 66 → your number is lesser
enter the guess: 88 → your number is correct
congraulations,you have guessed the number 88 in 3 attempts
Level 4 — Trial Exceeded Message
Adds a message when all 7 trials are exhausted without guessing correctly.
Code:
secret_code = 88
for i in range(0, 7):
user_input = int(input("enter the guess:"))
if user_input < 1 or user_input > 100:
print("invalid number: please enter number 1=100")
elif user_input > secret_code:
print("your number is higher")
elif user_input < secret_code:
print("your number is lesser")
else:
print("your number is correct")
break
if secret_code == user_input:
print(f"congradulations,you have guessed the number {secret_code} in {i}
attempts")
else:
print("sorry all your trial is exceeded")
Output (all trials failed):
enter the guess: 97 → your number is higher
sorry all your trial is exceeded
Level 4 Variant — Infinite Trials (while True)
Code:
secret_code = 88
while True:
user_input = int(input("enter the guess:"))
if user_input < 1 or user_input > 100:
print("invalid number: please enter number 1=100")
elif user_input > secret_code:
print("your number is higher")
elif user_input < secret_code:
print("your number is lesser")
else:
print("your number is correct")
break
Level 5 — Random Secret Code
Secret code is now random (not fixed). Introduces the random module.
Code:
import random
secret_code = [Link](1, 100)
for i in range(0, 7):
user_input = int(input("enter the guess:"))
if user_input < 1 or user_input > 100:
print("invalid number: please enter number 1=100")
elif user_input > secret_code:
print("your number is higher")
elif user_input < secret_code:
print("your number is lesser")
else:
print("your number is correct")
break
if secret_code == user_input:
print(f"congradulations,you have guessed the number {secret_code} in {i}
attempts")
else:
print("sorry all your trial is exceeded")
Sample Output:
enter the guess: 41 → your number is lesser
enter the guess: 70 → your number is lesser
enter the guess: 90 → your number is lesser
enter the guess: 95 → your number is higher
enter the guess: 93 → your number is higher
enter the guess: 91 → your number is lesser
enter the guess: 92 → your number is correct
congradulations,you have guessed the number 92 in 6 attempts
Level 5.2 — Random Code + Infinite Trials
Code:
import random
secret_code = [Link](1, 100)
while True:
user_input = int(input("enter the guess:"))
if user_input < 1 or user_input > 100:
print("invalid number: please enter number 1=100")
elif user_input > secret_code:
print("your number is higher")
elif user_input < secret_code:
print("your number is lesser")
else:
print("your number is correct")
break
Level 6 — Exception Handling
Handles invalid characters (non-numeric input) using try/except — ValueError.
Code:
import random
secret_code = [Link](1, 100)
for i in range(0, 7):
try:
user_input = int(input("enter the number:"))
if user_input < 1 or user_input > 100:
print("invalid number: please enter number 1=100")
elif user_input > secret_code:
print("your number is higher")
elif user_input < secret_code:
print("your number is lesser")
else:
print("your number is correct")
break
except ValueError:
print("Invalid characters,not [Link] enter number 1-100 to play
game")
if secret_code == user_input:
print(f"congradulations,you have guessed the number {secret_code} in {i}
attempts")
else:
print("sorry all your trial is exceeded")
Level 7 — Wrapped as a Function
The entire game is wrapped into a reusable function number_game() that runs when called.
Code:
def number_game():
import random
secret_code = [Link](1, 100)
for i in range(0, 7):
try:
user_input = int(input("enter the number:"))
if user_input < 1 or user_input > 100:
print("invalid number: please enter number 1=100")
elif user_input > secret_code:
print("your number is higher")
elif user_input < secret_code:
print("your number is lesser")
else:
print("your number is correct")
break
except ValueError:
print("Invalid characters,not [Link] enter number 1-100 to
play game")
if secret_code == user_input:
print(f"congradulations,you have guessed the number {secret_code} in {i}
attempts")
else:
print("sorry all your trial is exceeded")
# Call the function
number_game()