-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject2.py
70 lines (64 loc) · 2.27 KB
/
project2.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#guessing random number generated by computer
import random
def guess(n):
num=random.randint(1,n)
guess=0
while guess != num:
guess = int(input(f"Guess a num between 1 and {n}: "))
if guess < num:
print("Low")
elif guess > num:
print("HIGH")
print("Correct")
guess(10)
#computer guesses the user number with hints
x=int(input())
def computer(n):
low=1
high=n
feedback=''
a=5
for i in range(0,a): #setting a limited number of attempts
if(a!=2):
if low!=high:
guess= random.randint(low,high)
else:
guess = high
feedback=input(f'is {guess} correct : ').lower()
if feedback=='high':
high=guess-1
print(f"you have {a-1} chances left")
a=a-1
elif feedback == 'low':
low= guess+1
print(f"you have {a-1} chances left")
a=a-1
elif feedback=='c':
print("I WON !!")
break
else:
x=input("Can you give us a hint\n yes or no: ").lower()
if x=='yes':
lower=int(input('The nearest lower number in multiple of 10 of your number: '))
higher=int(input('The nearest higher number in multiple of 10 of your number: '))
for i in range(1,a):
if lower!=higher:
guess= random.randint(lower,higher)
else:
guess = higher
feedback=input(f'is {guess} correct : ').lower()
if feedback=='higher':
higher=guess-1
print(f"you have {a-1} chances left")
a=a-1
elif feedback == 'lower':
lower= guess+1
print(f"you have {a-1} chances left")
a=a-1
elif feedback=='c':
print("I won!!")
break
elif x=='no':
print("I accept defeat")
computer(x)
#generating a game to guess the number