Skip to content

Instantly share code, notes, and snippets.

@aindong
Last active May 22, 2018 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aindong/63e7994baa72885fa3279a8970d14778 to your computer and use it in GitHub Desktop.
Save aindong/63e7994baa72885fa3279a8970d14778 to your computer and use it in GitHub Desktop.
import random
def main():
# Generate digits
code = generate_digits(3, [])
print(code)
# Some story
print("Welcome to CodeBreaker Game, You need to unlock something before something happened")
print("Can you guess the code to unlock that something?")
# Game loop
while True:
# Get user input
user_input = input("Enter your guess: ")
# If input is less than 3
if len(user_input) < 3:
continue
# Check if user_input is correct
code_string = ''.join(str(x) for x in code)
if user_input == code_string:
print("You break the code")
break
# Check the input
check_result = check_input(user_input, code)
print(check_result)
def generate_digits(n, output):
if (n == 0):
return output
digit = random.randint(1, 9)
# Check if the generated digit
# is already on the output array
if digit in output:
return generate_digits(n, output)
# If no duplicate, append to output array
output.append(digit)
return generate_digits(n - 1, output)
def check_input(input, code):
result = "Nope"
for i in range(len(code)):
current_digit = int(input[i])
if current_digit in code:
if current_digit == code[i]:
return "Match"
return "Close"
return result
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment