为了账号安全,请及时绑定邮箱和手机立即绑定

如何在交互式shell中使用脚本

如何在交互式shell中使用脚本

慕妹3242003 2021-03-17 13:09:51
我写了下面的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


查看完整回答
反对 回复 2021-03-30
  • 3 回答
  • 0 关注
  • 113 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号