-
Notifications
You must be signed in to change notification settings - Fork 1
/
guess_the_no.py
63 lines (60 loc) · 2.06 KB
/
guess_the_no.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
'''
This is no guessing console game please see the code and
give me feedback for improvement.
I have used a random module to generating the no,I hope you will like it.
'''
import random
num_digits = 3
no_of_guess = 10
def main() :
print(f'''I am Rakesh Soni, Welcome to the game.
This is a no guessing game, system is thinking {num_digits} digits no without repeating.
system will give you the hints if you guess wrong no.
*******see carefully you need this while playing*******
Hint1: if one digit is correct but in wrong position
Hint2: one digit is correct and correct position
Rethink: no digit is correct''')
while True:
secretnum = getsecretnum()
print("i have a no.")
print(f"you have {no_of_guess} guess to get it.")
numguesses = 1
while numguesses <= no_of_guess:
guess = ''
while len(guess) != num_digits or not guess.isdecimal():
print(f"guess {numguesses}:")
guess = input('>')
hints = get_hint(guess,secretnum)
print(hints)
numguesses += 1
if guess == secretnum:
break
if numguesses > no_of_guess:
print("you run out of guesses")
print(f"the ans was{secretnum}")
print("do yoi want play again?(yes/no)")
if not input('.').lower().startswith("y"):
break
print("Well played! Thanks for play")
def getsecretnum():
numbers = list('0123456789')
random.shuffle(numbers)
secretnum = ''
for i in range(num_digits):
secretnum += str(numbers[i])
return secretnum
def get_hint(guess, secretnum):
if guess == secretnum:
return "you got it"
hints = []
for i in range(len(guess)):
if guess[i] == secretnum[i]:
hints.append("Hint1")
elif guess[i] in secretnum:
hints.append("Hint2")
if len(hints) == 0:
return "Rethink"
else:
hints.sort()
return ' '.join(hints)
main()