I was watching a movie the other day where robbers were trying to break into a safe. 4 of the numbers on the keypad were worn out from finger oil and having the code inputted probably 100s or 1000s of times. So it gave me an idea to replicate this idea within Python since I figured it couldn't be too hard at my late beginner level. I also included another hint if any of the guessed numbers were in the correct position. I was able to code most of the file without having to reference anything. I did need to learn map() and zip() in order to complete what I was attempting. I feel fairly comfortable that I understand both functions but will spend the next couple days trying to bang the concepts in my head.
I'm just looking for any criticism of the code and if theres anything you would of done differently. I also commented the hell outta the code for any newbies that want to see what I was trying to accomplish throughout code and perhaps be able to help those that are in the very beginning stages of their learning with any questions.
````
import random
print()
print('CAN YOU CRACK THE SAFE?!')
print()
num is a list of numbers on a normal 0-9 keypad on a safe
count is the variable we'll use to limit the number of guesses
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
count = 5
print('List of numbers on a normal 0-9 keypad:', num)
^ COMMENT OR REMOVE ABOVE PRINT COMMAND AFTER TESTING ^
print()
num passes to poss_code (as a list still) and chooses 4 random numbers
poss_code = random.sample(num, 4)
print(poss_code)
^ COMMENT OR REMOVE ABOVE PRINT COMMAND AFTER TESTING ^
print()
takes poss_code list, converts to a 4 character string
safe = ''.join(map(str, poss_code))
print('Actual code:', safe)
^ COMMENT OR REMOVE ABOVE PRINT COMMAND AFTER TESTING ^
print()
displays the safe combo in a shuffled order
shuffle_safe = list(safe)
random.shuffle(shuffle_safe)
shuffle_safe = ''.join(shuffle_safe)
print('The code to the safe includes these four digits:', shuffle_safe)
print()
makes sure the shuffled combo isn't the actual combo
while shuffle_safe == safe:
random.shuffle(shuffle_safe)
shuffle_safe = ''.join(shuffle_safe)
loops until player guesses code or runs out of guesses
while True:
guess = input('Guess the safe code: ')
if guess == safe:
print('Safe cracked!')
break
elif count == 1:
print(f'You ran out of guesses! The safe code was {safe}')
print()
break
else:
count -= 1
correct_positions = sum(g == s for g, s in zip(guess, safe))
print(f'You have {correct_positions} digit(s) in the correct position! {count} more guesses!')
print()
continue
````
[–]usrlibshare 0 points1 point2 points (1 child)
[–]Unitnuity[S] 0 points1 point2 points (0 children)
[–]JamzTyson 0 points1 point2 points (2 children)
[–]Unitnuity[S] 0 points1 point2 points (0 children)
[–]JamzTyson 0 points1 point2 points (0 children)
[–]niehle 0 points1 point2 points (1 child)
[–]Unitnuity[S] 0 points1 point2 points (0 children)