Zhall
Zhall

Reputation: 89

Python wont define function

I am getting back into python and I'm having a really basic issue....

My source has the following...

def calrounds(rounds):
    print rounds

When I run this through the shell and try to call calrounds(3) I get..

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    calrounds(3)
NameError: name 'calrounds' is not defined

Its been awhile since I've used python, humor me.

Upvotes: 0

Views: 260

Answers (3)

Sean McSomething
Sean McSomething

Reputation: 6507

The first thing to do is to look at how you're calling the function. Assuming it's in myModule.py, did you import myModule or did you from myModule import calrounds? If you used the first one, you need to call it as myModule.calrounds().

Next thing I would do is to make sure that you're restarting your interpreter. If you have imported a module, importing it again will not reload the source, but use what is already in memory.

The next posibility is that you're importing a file other than the one you think you are. You might be in a different directory or loading something from the standard library. After you import myModule you should print myModule.__file__ and see if it is the file you think you're working on. After 20 years of programming, I still find myself doing this about once a year and it's incredibly frustrating.

Finally, there's the chance that Python is just acting up. Next to your myModule.py there will be a myModule.pyc - this is where Python puts the compiled code so it can load modules faster. Normally it's smart enough to tell if your source has been modified but, occassionally, it fails. Delete your .pyc file and restart the interpreter.

Upvotes: 1

Antony Hatchkins
Antony Hatchkins

Reputation: 33994

Did you import your source first?

Upvotes: 2

Konstantin Dinev
Konstantin Dinev

Reputation: 34895

It says that the first line of your program is calling calrounds with a parameter 3. Move that below your function definition. The definition needs to be before you call the function. If you are using python 3.0+ you need parenthesis for the print statement.

>>> def calrounds(rounds):
    print(rounds)


>>> calrounds(3)
3 

Upvotes: 1

Related Questions