Here’s an example of a simple gaming program in Python that simulates a guessing game:
import random
def guess_number():
number = random.randint(1, 100)
attempts = 0
guessed = Falseprint(“Welcome to the Number Guessing Game!”)
print(“———————————–“)
print(“I’m thinking of a number between 1 and 100. Can you guess it?”)while not guessed:
guess = int(input(“Enter your guess: “))
attempts += 1if guess < number: print(“Too low! Try again.”) elif guess > number:
print(“Too high! Try again.”)
else:
print(“Congratulations! You guessed the number in”, attempts, “attempts.”)
guessed = Truedef main():
play_again = “yes”while play_again.lower() == “yes” or play_again.lower() == “y”:
guess_number()
play_again = input(“Do you want to play again? (yes/no): “)print(“Thank you for playing!”)
if __name__ == ‘__main__’:
main()
In this program, the guess_number
function generates a random number between 1 and 100. It then prompts the user to input their guess and provides feedback if the guess is too low or too high. Once the user correctly guesses the number, it displays the number of attempts made and congratulates the player. The main
function allows the player to play the game multiple times by asking if they want to play again.
When you run the program, it will welcome the player to the Number Guessing Game and prompt them to input their guesses. After the player guesses the correct number, it will display the number of attempts and ask if they want to play again. The game continues until the player chooses not to play again.
Feel free to enhance the game further by adding additional features, such as keeping track of high scores or implementing difficulty levels.