Created
February 23, 2019 16:57
-
-
Save rosdyana/de8ae9b8754de5e26bc5da82507302fa to your computer and use it in GitHub Desktop.
mini game with python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def set_user_guess(): | |
return input("What is your guess ? ") | |
def generate_secret(): | |
secret_code = [str(num) for num in range(10)] | |
random.shuffle(secret_code) | |
return secret_code[:3] | |
def check_code(userinput, secretcode): | |
userinput = list(userinput) | |
if userinput == secretcode: | |
return "CODE CRACKED!" | |
results = [] | |
for i, v in enumerate(userinput): | |
if v == secretcode[i]: | |
results.append("it's a match!") | |
elif v in secretcode: | |
results.append("it's close, one of your number is our secret code.") | |
if results == []: | |
return ["Nope, try your luck, dude!"] | |
else: | |
return results | |
results = [] | |
secretcode = generate_secret() | |
while results != "CODE CRACKED!": | |
print("We have 3 secret numbers, can you guess it?") | |
userinput = set_user_guess() | |
if len(userinput) == 3 and str.isdigit(userinput): | |
results = check_code(userinput, secretcode) | |
for result in results: | |
print(result) | |
else: | |
print("Only accept 3 numbers!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment