我写了下面的python程序#! /usr/bin/pythondef checkIndex(key): if not isinstance(key, (int, long)): raise TypeError if key<0: raise IndexErrorclass ArithmeticSequence: def __init__(self, start=0, step=1): self.start = start # Store the start value self.step = step # Store the step value self.changed = {} # No items have been modified def __getitem__(self, key): checkIndex(key) try: return self.changed[key] except KeyError: return self.start + key*self.step def __setitem__(self, key, value): checkIndex(key) self.changed[key] = value我做的时候程序是my.pychmod +x my.pypython my.py在执行完此步骤后,我将回到bash shelluser@ubuntu:~/python/$ pythonPython 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> s=ArithmeticSequence(1,2)Traceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'ArithmeticSequence' is not defined我如何给程序输入并运行它,因为它已保存在vi中
3 回答

拉丁的传说
TA贡献1789条经验 获得超8个赞
好吧,您要么必须使用以下程序作为程序来运行它
if __name__ == 'main':
# Your code goes here. This will run when called from command line.
或者,如果您在python解释器中,则必须使用以下命令导入“ my”:
>>> import my
添加回答
举报
0/150
提交
取消