我寫了下面的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我做的時(shí)候程序是my.pychmod +x my.pypython my.py在執(zhí)行完此步驟后,我將回到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我如何給程序輸入并運(yùn)行它,因?yàn)樗驯4嬖趘i中
3 回答

拉丁的傳說(shuō)
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
好吧,您要么必須使用以下程序作為程序來(lái)運(yùn)行它
if __name__ == 'main':
# Your code goes here. This will run when called from command line.
或者,如果您在python解釋器中,則必須使用以下命令導(dǎo)入“ my”:
>>> import my
添加回答
舉報(bào)
0/150
提交
取消