pythonpython-3.xrandompasswordsadventure

Python 3: How to use a random number generator code to be used as a password for a door


I am making a text adventure game in Python 3 and I am trying to use a random code found in one part of the game and used as a password for the final part of the game but I don't know how to make the random generated code the password. I have the random number generator. How do I make the number that is generated the password.

import random
def code():
     numbers = random.sample(range(10), 4)
     print(''.join(map(str, numbers)))

Solution

  • Make your function not print, but return the code:

    def code():
        numbers = random.sample(range(10), 4)
        return ''.join(map(str, numbers))
    

    and in your program, store it to a variable, so that you can both print and query for it:

    # ...
    password = code()
    print(password)
    # ...
    check = input('Enter password:\n')
    if check == password:
        # yeah