Skip to content

Commit c00b478

Browse files
committed
1st commit
1 parent 5284ddc commit c00b478

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

nf.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import random
2+
def mastermind():
3+
colours = ["R", "G", "B", "Y", "W", "O"] #R-RED,G-GREEN,B-BLUE,Y-YELLOW,W-WHITE,O-ORANGE
4+
attempts = 0
5+
game = True
6+
s=1
7+
colour_code = random.sample(colours,4)
8+
print ("START GUESSING")
9+
while game:
10+
correct_colour = ""
11+
guessed_colour = ""
12+
player_guess=""
13+
player_guess=raw_input().upper()
14+
attempts+= 1
15+
if len(player_guess) != len(colour_code):
16+
print ("The secret code needs to have exactly four colors")
17+
continue
18+
for i in range(4):
19+
if player_guess[i] not in colours:
20+
print ("invalid input")
21+
continue
22+
if correct_colour != "bbbb":
23+
for i in range(4):
24+
if player_guess[i] == colour_code[i]:
25+
correct_colour += "b"
26+
if player_guess[i] != colour_code[i] and player_guess[i] in colour_code:
27+
guessed_colour += "w"
28+
print (correct_colour + guessed_colour + "\n")
29+
30+
if correct_colour == "bbbb":
31+
if attempts == 1:
32+
print ("You guessed at the first attempt!")
33+
else:
34+
print ("Well done... You needed " + str(attempts) + " attempts to guess.")
35+
game = False
36+
37+
if attempts >= 1 and attempts <6 and correct_colour != "bbbb":
38+
print ("Next attempt: ")
39+
elif attempts >= 6:
40+
print ("You didn't guess! The secret color code was: " + str(colour_code))
41+
42+
#To play or not to play
43+
while game == False:
44+
finish_game = input("Do you want to play again (Y/N)?").upper()
45+
attempts = 0
46+
if finish_game =="N":
47+
print ("Thanks for the game")
48+
elif finish_game == "Y":
49+
game = True
50+
print ("Let's play again.Guess the secret code: ")

0 commit comments

Comments
 (0)