0

我正在重新使用 python,但我遇到了一个非常基本的问题....

我的来源有以下...

def calrounds(rounds):
    print rounds

当我通过外壳运行它并尝试调用 calrounds(3) 我得到..

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

自从我使用python以来已经有一段时间了,请幽默。

4

3 回答 3

2

你是你import的来源吗?

于 2013-02-05T17:24:03.350 回答
1

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.

于 2013-02-05T17:32:48.877 回答
1

它说你的程序的第一行是calrounds用一个参数调用的3。将其移至函数定义下方。定义需要在调用函数之前。如果您使用的是 python 3.0+,则 print 语句需要括号。

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


>>> calrounds(3)
3 
于 2013-02-05T17:20:18.183 回答