-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise5.py
More file actions
30 lines (24 loc) · 839 Bytes
/
exercise5.py
File metadata and controls
30 lines (24 loc) · 839 Bytes
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
def guess_number():
import random
# Computer selects a secret number between 1 and 20
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20.")
# Initialize the number of tries
tries = 0
while True:
# User guesses the number
guess = int(input("Take a guess: "))
# count the number of tries
tries += 1
# Check if the guess is correct
if guess == secret_number:
print(f"Correct! You guessed it in {tries} tries.")
break
# Check if the guess is too high
elif guess > secret_number:
print("Too high!")
# Check if the guess is too low
else:
print("Too low!")
# Call the function
guess_number()