acars123 acars123 - 17 days ago 5
Python Question

Make a locking-like variable in Python?

I'm asking if there's a way to essentially block off the rest of the program and only have access to a limited set of the program, for example

if speech.said("Lock"):
Lock = true
speech.say("Locked");

if Lock = true:
[The continuation of this is my issue.]


How do I continue this portion of the code? I'm completely new to Python as well, I've only had 3 days working with it. If you don't mind explaining thoroughly, any advice would be greatly appreciated!
I'm making this with FreePIE, a programmable input emulator. It's much more flexible than the original GlovePIE, which has been discontinued, and Python is much more complex than I anticipated, I've been stuck on the lock issue for 2 days.. so literally anything is better than what I've got now, any simple solution to workarounds.

Thanks again, Austin.

Answer

Here is an example using python speech and a basic loop:

import string
import speech

locked = False

while True:
    print "Talk:"
    phrase = speech.input()
    if phrase.lower() == "unlock":
        locked = False
    if phrase.lower() == "lock":
        speech.say("Locked")
        locked = True  
    if locked: 
        continue
    print "You said {0}".format(phrase)          
    if phrase.lower() == "exit":
        break

In here, we loop infinitely until the user types 'Exit'. During the loop we:

  • check if the user requested an unlock, if they did we unlock
  • check if the user requested a lock, if so, we set lock to True
  • check if we are locked: if we are, we ignore the input and immediately restart the loop -- thats what continue means
Comments