-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (36 loc) · 1.25 KB
/
main.py
File metadata and controls
51 lines (36 loc) · 1.25 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import random
while True:
turns_left = 8
print("Welcome to Mastermind!\nYou have", turns_left,
"turns to try and guess the secret number (between 100 and 999)!")
secret = str(random.randint(100, 999))
while turns_left > 0:
guess = input("Guess a number:")
while len(guess) != 3:
print("Must be a number 100-999")
guess = input("Guess a number: ")
if guess == secret:
print("You got it!")
break
number_correct = 0
position_correct = 0
guess_list = []
secret_list = []
for g, s in zip(guess, secret):
if g == s:
position_correct += 1
else:
guess_list.append(g)
secret_list.append(s)
for g in guess_list:
if g in secret_list:
number_correct += 1
secret_list.remove(g)
print("That guess got", position_correct, "numbers in the right position and", number_correct,
"other correct numbers.")
turns_left -= 1
if turns_left == 0:
print("Good try! The secret number was", secret)
finish = input("Play again? [y/n]:").lower()
if finish != 'y':
break