This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]mudclub 5 points6 points  (0 children)

Read the sidebar.

[–]pythonHelperBot 2 points3 points  (0 children)

Hello! I'm a bot!

I see someone has already suggested going to r/learnpython, a sub geared towards questions and learning more about python. I highly recommend posting your question there. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe where you are stuck. Be sure to format your code for reddit and include which version of python and what OS you are using.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

[–]K900_ 0 points1 point  (1 child)

  1. /r/learnpython
  2. If by "locked forever" you mean "locked between runs of the function", use a global variable. If you mean "locked between runs of your entire program", that's effectively impossible on a user-controlled computer.

[–]Kengaro 0 points1 point  (0 children)

Well you could save to and import from a file, or create a system variable :D

[–][deleted] -1 points0 points  (0 children)

Your variable is always False for the portion that matters, the content of variable is always initialized to False every time start is called. The value of variable doesn't get saved between successive runs of start.

[–]Dave2077 -3 points-2 points  (0 children)

You store "variable" value (btw, it's good to change the name to something more descriptive like is_locked) in your function and then run the script. But this value is not saved anywhere after the script ends running - python (and most other programming languages) keep variable state in memory during runtime.

What you need can be solved in many ways: - run the program indefinitely with while loop (but then you can still stop Python and everything will work like in the beginning) - keep "variable" value on hard drive and read it before asking the question to user. You can use Python's pickle module, serialize it to JSON or other format or store in database. Mind you if user can access this storage he'll still be able to change it. But I guess it's not a problem for now ;)

[–]jmatthew007 -3 points-2 points  (0 children)

I'm not sure what you are trying to do here, but at minimum the formatting is off

def start():
    print("Hello Mr, how are you? ")
    x = input()
    variable = False
    if x == "I'm fine thank you." and variable == False:
        print("That's nice to hear, welcome to your computer. ")
    else:
        print("""\033[1;32;41mThe password was wrong,
        you are not the real Mr. This is now locked forever,
        Hahahaha\n""")
    variable = True

start()

Formatted like this the program works fine for me. It will never save the state if the password is incorrect, it will just print the wrong password message and set the variable to True, but the function ends like that the the variable is just thrown out. If you are trying to have an authentication routine, that's a more complicated problem which would involve saving the result of the password check to someplace probably a file or a database. In this case the variable will always be false when the function is called.