Skip to content

Instantly share code, notes, and snippets.

@haridutt12
Created January 29, 2019 11:39
Show Gist options
  • Save haridutt12/ec7a594c70ab41ce282e4d97e5dd1873 to your computer and use it in GitHub Desktop.
Save haridutt12/ec7a594c70ab41ce282e4d97e5dd1873 to your computer and use it in GitHub Desktop.
###########################
## 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!
# There are a few things you will have to discover for yourself for this game!
# Here are some useful hints:
# Try to figure out what this code is doing and how it might be useful to you
import random
def get_guess():
return list(input("What is your guess?"))
def generate_code():
digits = [str(num) for num in range(10)]
random.shuffle(digits)
return digits[:3]
def generate_clues(code,userGuess):
if userGuess == code:
return "CODE CRACKED"
clues = []
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
print("Welcome Code Breaker! Let's see if you cint(guess[2]) == digits[2] oran guess my 3 digit number!")
secretCode = generate_code()
print("Code has been generated, please guess a 3 digit number")
clueReport = []
while clueReport != "CODE CRACKED":
guess = get_guess()
clueReport = generate_clues(guess,secretCode)
print("Here is the result of your guess:")
for clue in clueReport:
print(clue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment