-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguess.py
More file actions
32 lines (28 loc) · 1.05 KB
/
guess.py
File metadata and controls
32 lines (28 loc) · 1.05 KB
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
import random
#Guess the number (user)
def guess(x):
random_number = random.randint(1,x)
guess = 0
while guess != random_number:
guess = int(input(f'Guess a number between 1 and {x} : '))
if guess < random_number:
print("Sorry guess again. It's too low")
if guess > random_number:
print("Sorry guess again. It's too high")
print(f"Congratulations!. You've guessed the number correctly {random_number}")
def computer_guess(x):
low = 1
high = x
feedback = ''
while feedback != 'c':
if low != high:
guess = random.randint(low,high)
else:
guess = low #low = high
feedback= input(f"Is {guess} too high (h), low (l) or correct (c)? ")
if feedback == 'h':
high = guess - 1
elif feedback == 'l':
low = guess + 1
print(f"Congrats, The computer guessed your number {guess} correctly")
computer_guess(10)