In my program, I'm trying to run a "def command" in a conditional statement. An error pops up saying name 'command' is not defined.
I tried to rewrite the code and tried to reload the repl.it server many times.
def main():
inputmain = input("...")
if inputmain == "Yes" or "yes" or "y" or "Y":
command()
elif inputmain == "No" or "no" or "n" or "N":
print("Ok.")
else:
print("Error")
main()
def command():
...
...
command()
I expect the output of "y" to be the program command() but it's the error above.
The comments were growing long. Here is an amended version of your code. As you can see, I am not calling command()
anywhere except in the main()
function.
The command()
function does not need to be defined before main()
is defined. It just needs to be defined before main()
is executed because main()
may potentially call on it.
def main():
inputmain = input("...")
if inputmain in ("Yes", "yes", "y", "Y"):
command()
elif inputmain in ("No", "no", "n", "N"):
print("Ok.")
else:
print("Error")
def command():
print(1)
main()