My question is pretty simple how do how to declare value as none the redeclare it then use it for an if statement in python?
global command
command = None
def user_commands():
def take_command():
try:
with sr.Microphone() as source:
print('listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'alexa' in command:
command = command.replace('alexa', '')
print(command)
except:
pass
return command
def run_alexa():
global command
command = user_commands()
...
if 'Play' in command:
...
what is happening when I do this is: TypeError: argument of type 'NoneType' is not iterable
What I am doing is creating an alexa which ask to take commands then do the command. the variable for the command is set equal to the person says but in python it doesnt like to have a variable without a type so I said no then said ill redeclare it but it does not work,
In your particular case, you are using the variable before you set it - which is not exclusive to
globalvariables. That said; you may be confused on how theglobalkeyword works.globalallows you to create, or modify, a variable from within an enclosed scope, that will be elevated to the global scope.For example, you can try this in your REPL:
If you use the
globalkeyword, however, it will be set when you modify it in the function.