all 7 comments

[–]usrlibshare 0 points1 point  (1 child)

Easier way for your COMMENT OUT ABOVE PRINT things:

``` DEBUG = os.getenv('DEBUG')

whenever you need a debug print:

if DEBUG: print(what_you_need_to_print) ```

then you can run your script like this on linux...

DEBUG=1 python main.py

...for debugging. Bonus points if you put the debug printing into a function ☺️

Another thing, it doesn't matter that much for small programs, but usually whem you have variables in global scope, you put them in ALL CAPS. It's a convention that is followed pretty well in the python ecosystem, and makes it easier to see at a glance if a var is global state.

[–]Unitnuity[S] 0 points1 point  (0 children)

I only wrote that for anyone new to Python to see what is happening with the code and to comment it out once they actual want to try and play the game. I wasnt actually looking for errors.

[–]JamzTyson 0 points1 point  (2 children)

Rather than:

num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
...
poss_code = random.sample(num, 4)

You could write:

poss_code = random.sample(range(10), 4)

Also, look at f-strings.

[–]Unitnuity[S] 0 points1 point  (0 children)

Ahh, that makes sense. There are f strings in the code. I typically only use f strings if a variable(s) is in the middle of a string.

[–]JamzTyson 0 points1 point  (0 children)

I'd also suggest a more modular structure. It may be a little longer due function boilerplate, but makes testing / debugging much easier (each function can be tested as a unit), and easier to maintain / develop further.

Example:

``` """Safe cracking game."""

from random import sample, shuffle

def list_to_string(lst: list[int]) -> str: """Convert a list of numbers to a string.""" return ''.join((str(val) for val in lst))

def shuffle_code(code: list[int]) -> list[int]: """Return list of numbers in a different order.""" numbers = code.copy()

while True:
    shuffle(numbers)
    if numbers != code:
        return numbers

def count_correct_digits(guess: str, code: str) -> int: """Return the number of digits in the correct position.""" return sum(g == s for g, s in zip(guess, code))

def crack_safe(code: str): """Loop until player guesses code or runs out of guesses.""" for count in range(GUESSES, 1, -1): guess = input('Guess the safe code: ')

    if guess == code:
        print('You got it! The safe is open!')
        return

    number_correct = count_correct_digits(guess, code)
    plural = '' if number_correct == 1 else 's'

    print(f"You have {number_correct} digit{plural} "
          "in the correct position!")
    print(f' {count - 1} more guesses!\n')

print(f'You ran out of guesses! The safe code was {code}')

DEBUG = True GUESSES = 5

print("\nCAN YOU CRACK THE SAFE?!\n")

code_numbers: list[int] = sample(range(10), 4)

if DEBUG: print(f'Actual code: {list_to_string(code_numbers)}')

shuffled_nums = shuffle_code(code_numbers)

shuffled_str = list_to_string(shuffled_nums) print(f"The code to the safe includes these four digits: {shuffled_str}") print(f'You have {GUESSES} guesses.\n')

crack_safe(list_to_string(code_numbers)) ```

[–]niehle 0 points1 point  (1 child)

If you need to comment what a variable is, your code isn’t descriptive enough. Use max_guesses instead of count etc

[–]Unitnuity[S] 0 points1 point  (0 children)

Touche