Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function in python, getting '(function) is not defined'? [duplicate]

I recently started learning Python and have some code here.

...
workout = input("Work out if you won?")

if workout == "y":
    ballone()
elif workout == "n":
    print("Okay.")
    sys.exit("Not working out if you won")
else:
    sys.exit("Could not understand")

##Ball one
def ballone():
...

The issue is calling 'ballone'. You can see that it is defined and works perfectly when called from the command line (ballone())

Any ideas? I have scoured the net but cannot seem to find anything to help me. If any more code needs posting then please let me know :)

like image 551
Mattios550 Avatar asked Dec 20 '22 09:12

Mattios550


1 Answers

Move the function definition to before the lines that use it.

def ballone():
    # ...

if workout == "y":
    ballone()
elif workout == "n":
    print("Okay.")
    sys.exit("Not working out if you won")
else:
    sys.exit("Could not understand")

Functions are stored in identifiers (variables), just like your workout value. If you don't define it first, how is Python to know it'll be defined later?

like image 93
Martijn Pieters Avatar answered Dec 24 '22 01:12

Martijn Pieters