首页 新闻 会员 周边

python问题

0
[已解决问题] 解决于 2016-10-09 11:52
#!/usr/bin/env  python
#encoding:UTF-8
def showmenu():
        tmp="""
        (1)取五个数的和
        (2)取5个数的平均值
        (X)退出
        enter you choice:"""

def adder():
        alist=[]
        for i  in range(5):
                alist.append(int(raw_input('plesae input:')))
        return  sum(alist)
def diver():
        alist=[]
        for i in range(5):
                alist.append(int(raw_input('please input:')))
        sum1=float(sum(alist))
        avg=sum1/len(alist)
        return  avg
cmd={'1':adder(),'2':diver}
while True:
        while True:
                try:
                        choice=raw_input(tmp).strip()[0].lower()
                except  (EOFError):
                        choice='x'
                if  not choice  in '12x':
                        print  'invalid  option'
                else:
                        break
        if choice=='x':
                break
        cmd[choice]()
if  __name__=='__main__':
        showmenu()
Traceback (most recent call last):
  File "2-11.py", line 26, in <module>
    choice=raw_input(tmp).strip()[0].lower()
NameError: name 'tmp' is not defined

谁可以告诉我为什么总是显示我tmp没有定义啊?????

窄小的空间的主页 窄小的空间 | 菜鸟二级 | 园豆:250
提问于:2016-09-04 11:19
< >
分享
最佳答案
0

在函数内定义的变量默认是局部变量,其它函数不能读取。

具体看教程 和搜索  局部变量和全局变量的概念。

奖励园豆:5
墨镜带佬星 | 老鸟四级 |园豆:2294 | 2016-09-14 12:32
其他回答(1)
1

tmp在函数里面 为局部变量。global 声明一下为全局变量

Mr_Cxy | 园豆:210 (菜鸟二级) | 2016-09-20 06:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册