-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess4.py
82 lines (77 loc) · 1.66 KB
/
guess4.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
71
72
73
74
75
76
77
78
79
80
81
82
f=open('highscore1.txt','r')
highscore=int(f.read())
f.close()
import random
buf=[]
def caller(a):
global buf
c=random.randint(0,a-1)
if c not in buf:
buf.append(c)
return c
else:
return caller(a)
def check(a):
global sol
global buf
count=0
b=list(str(a))
for j in range(l):
i=caller(l)
if b[i] in sol and b[i]==sol[i]:
print ('ga',end=' ')
count+=1
elif b[i] in sol and b[i]!=sol[i]:
print ('re',end=' ')
elif b[i] not in sol:
print ('sa',end=' ')
print ('\r')
buf=[]
return count
def guessing():
global counter
global l
global n
for i in range(20):
counter+=1
n=int(input('Enter a number :'))
try:
if len(str(n))==l:
p=check(n)
if p==l:
print('You guessed it right !')
print ('Your Score is :',counter)
if counter<highscore:
print('You have set a new highscore !')
f=open('highscore1.txt','w')
f.write(str(counter))
f.close()
else:
print ('highscore is :',highscore)
except:
pass
finally:
if len(str(n))!=l:
print ('Please enter',l,'digit number')
elif p==l:
break
else:
print('Sorry, tries over')
confess=input('Do you want more tries ? (y/n)')
if confess=='y':
guessing()
else:
print ('The correct number is :',num)
ans='y'
while ans=='y':
print('Welcome to the guessing game')
print ('sa : wrong number')
print ('re : correct number, wrong position')
print('ga : correct number, correct position')
l=int(input('Enter number of digits of the number you want to guess: '))
num=random.randint(10**(l-1),(10**l)-1)
sol=list(str(num))
print ('Guess a',l,'digit number')
counter=0
guessing()
ans=input('Do you want to play again ? (y/n)')