-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandino.py
executable file
·41 lines (33 loc) · 1.25 KB
/
randino.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 get_valid_guess():
while True:
guess = input("Enter your guess (or type 'quit' to exit): ")
if guess.strip().lower() == "quit":
print("Exiting the game. Goodbye!")
exit() # Exit the game if the user enters "quit"
if guess.strip() == "":
print("Please enter a valid number.")
else:
try:
guess = int(guess)
return guess
except ValueError:
print("Invalid input. Please enter a valid number.")
def number_guessing_game():
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100. Can you guess it?")
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0
while True:
guess = get_valid_guess()
attempts += 1
if guess < secret_number:
print("Too low! Try guessing a higher number.")
elif guess > secret_number:
print("Too high! Try guessing a lower number.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts!")
break
# Call the function to start the game
number_guessing_game()