-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part10_Simple_Game_SOLUTIONS.py
82 lines (67 loc) · 2.33 KB
/
Part10_Simple_Game_SOLUTIONS.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
###########################
## PART 10: Simple Game ###
### --- CODEBREAKER --- ###
## --Nope--Close--Match--##
###########################
# It's time to actually make a simple command line game so put together everything
# you've learned so far about Python. The game goes like this:
# 1. The computer will think of 3 digit number that has no repeating digits.
# 2. You will then guess a 3 digit number
# 3. The computer will then give back clues, the possible clues are:
#
# Close: You've guessed a correct number but in the wrong position
# Match: You've guessed a correct number in the correct position
# Nope: You haven't guess any of the numbers correctly
#
# 4. Based on these clues you will guess again until you break the code with a
# perfect match, the game will report "CODE CRACKED"!
# There are a few things you will have to discover for yourself for this game!
# Here are some useful hints:
import random
def get_guess():
'''
Asks for the number guess and transforms the string to a list.
'''
return list(input("What is your guess?"))
def generate_code():
'''
generates a 3 digit list for the code
'''
digits = [str(num) for num in range(10)]
random.shuffle(digits)
return digits[:3]
def generate_clues(code,userGuess):
'''
Takes in a user guess and code then compares the numbers in a loop and
creates a list of clues according to the matching parameters.
'''
if userGuess == code:
return "CODE CRACKED"
clues = []
# Compare guess to code
for ind,num in enumerate(userGuess):
if num == code[ind]:
clues.append("Match")
elif num in code:
clues.append("Close")
if clues == []:
return ["Nope"]
else:
return clues
# Run Game
print("Welcome Code Breaker! Let's see if you can guess my 3 digit number!")
# Create a Secret Code to start the Game
secretCode = generate_code()
print("Code has been generated, please guess a 3 digit number")
#print(secretCode)
# Empty Clue Report to Start with
clueReport = []
# Keep asking until the user has gotten it right!
while clueReport != "CODE CRACKED":
# Ask for guess
guess = get_guess()
# Give the clues
clueReport = generate_clues(guess,secretCode)
print("Here is the result of your guess:")
for clue in clueReport:
print(clue)