4

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 :)

0

1 Answer 1

12

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?

Sign up to request clarification or add additional context in comments.

10 Comments

Typically this is done by putting the main logic in a function and calling it at the bottom of the module, usually guarded by if __name__ == '__main__':.
@StevenRumbalski: the __name__ test won't protect you from this error though. If you do that at the top of your module, you'll still have the same problem.
True, but I did qualify it that way.
@StevenRumbalski: So you did.
That seems really obvious now! :/ I have experience with PHP, HTML, CSS, jQuery etc... thanks for the other answers :D & for being so fast. I used to use Yahoo Answers for this type of question but this is my home now ;) :D
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.