forked from rksoni5967/python_console_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_the_no.py
67 lines (54 loc) · 1.96 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
64
65
66
67
import random
NUM_DIGITS = 3
MAX_GUESSES = 10
def main():
print('''Welcome.
This is a number guessing game where the system is thinking of a {}-digit number without repeating digits.
The system will give you hints if you guess the wrong number.
*******See carefully, you need this while playing*******
Hint1: one digit is correct but in the wrong position
Hint2: one digit is correct and in the correct position
Rethink: no digit is correct'''.format(NUM_DIGITS))
while True:
secret_num = get_secret_num()
print("I have thought of a number.")
print("You have {} guesses to get it.".format(MAX_GUESSES))
num_guesses = 1
while num_guesses <= MAX_GUESSES:
guess = ''
while len(guess) != NUM_DIGITS or not guess.isdigit():
print("Guess {}:".format(num_guesses))
guess = input('> ')
hints = get_hint(guess, secret_num)
print(hints)
num_guesses += 1
if guess == secret_num:
break
if num_guesses > MAX_GUESSES:
print("You've run out of guesses.")
print("The answer was {}".format(secret_num))
print("Do you want to play again? (yes/no)")
if not input('> ').lower().startswith("y"):
break
print("Well played! Thanks for playing.")
def get_secret_num():
numbers = list('0123456789')
random.shuffle(numbers)
secret_num = ''.join(numbers[:NUM_DIGITS])
return secret_num
def get_hint(guess, secret_num):
if guess == secret_num:
return "You got it!"
hints = []
for i in range(len(guess)):
if guess[i] == secret_num[i]:
hints.append("Hint2")
elif guess[i] in secret_num:
hints.append("Hint1")
if len(hints) == 0:
return "Rethink"
else:
hints.sort()
return ' '.join(hints)
if __name__ == "__main__":
main()