forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
41 lines (33 loc) · 1.12 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import random
def play_game():
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
while True:
# Get the player's guess
try:
guess = int(input("Enter your guess: "))
except ValueError:
print("Please enter a valid number.")
continue
attempts += 1
# Check the guess and provide feedback
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts!")
break
def main():
while True:
play_game()
# Ask if the player wants to play again
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != 'yes':
print("Thanks for playing! Goodbye!")
break
if __name__ == "__main__":
main()