Python is a widely used programming language for development that allows developers to create a wide range of applications, which also include games. In this article, we will learn step-by-step how to build a number-guessing game along with the code and output.
Setting the Game Level:
The game will ask the user to enter their desired level. If the players enter the level of 0, the game displays an error message and ends.
- The code prompts the players to enter their desired level from the number-guessing game.
- If players enter 0 the game will show an error.
- The number of remaining attempts for the player is calculated based on the level
- If the player successfully guessed the number, they will receive a congratulatory message and bonus points.
Code Building a Number Guessing Game
import random max_value =int(input('Enter your level : ')) if max_value==0: print("\n\tLevel zero is not available\n\t\tGame over\n\nError : ") remaining_attempts=float(1) remaining_attempts=75%max_value player_state=bool(False) bot_state=bool(False) if max_value<=10: level="Easy" elif max_value>10 and max_value<=40: level="Hard" else: level="Impossible" remaining_attempts=remaining_attempts -15% remaining_attempts print('Level applied : ',level,"\nYou got ", remaining_attempts," attempts") number= random.randint(1, max_value) bot_input=random.randint(1,max_value) for I in range(remaining_attempts): remaining_attempts=remaining_attempts-1 try: guess = int(input('\nEnter your guess ')) if bot_input==number: bot_state=True if guess > number: print('Too high') if guess < number: print('Too low') if guess==number: player_state=True bonus=float(remaining_attempts+10) remaining_attempts=remaining_attempts+bonus print("\n Congratulations,You got ",bonus," bonus points") break except ValueError: print("\nInvalid Input\n") remaining_attempts=remaining_attempts+1 print("\nGame over") if remaining_attempts==0: print("You loose the game\n")