小白教程

 找回密码
 立即注册
查看: 6403|回复: 2

调用函数时,输入未定义的参数,如何用try except打印出异...

[复制链接]

2

主题

3

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2021-3-5 20:30:40 | 显示全部楼层 |阅读模式
  1. user_list = [["Account number","Currency","Balance"],
  2.              ["001-987654-003","USD",1300.72],
  3.              ["001-919202-100","EUR",105.01],
  4.              ["001-905700-100","EUR",305.00],
  5.              ["001-908415-307","CHF",20804.98],
  6.              ["011-974777-200","PLN",208.15],
  7.              ["001-931654-001","USD",450.7]]

  8. def vlookup(lookupvalue,lookuplist,col_index):
  9.       
  10.     try:
  11.         
  12.         lookuplist = user_list
  13.         
  14.     except Exception as e:
  15.         
  16.         print(str(e))
  17.         
  18.     else:
  19.                  
  20.         length1 =  len(lookuplist[0])
  21.            
  22.         length2 =  len(lookuplist)      
  23.                
  24.         for j in range(length2):
  25.            
  26.             if lookupvalue == lookuplist[j][0]:
  27.                
  28.                
  29.                 if col_index - 1 in range(length1):
  30.                
  31.                         return print(lookuplist[j][col_index - 1])                                      
  32.                   
  33.                 else:           
  34.                         return print('Out of scope')               
  35.                
  36.             else:
  37.                
  38.                 continue
  39.                
  40.         return print('Not found')
复制代码
vlookup("001-919202-100",otheruserlist, 2)

Traceback (most recent call last):

  File "C:\Users\Kevin Sun\untitled0.py", line 59, in <module>
    vlookup("001-919202-100",otheruserlist, 2)

NameError: name 'otheruserlist' is not defined
回复

使用道具 举报

0

主题

3

帖子

5

积分

新手上路

Rank: 1

积分
5
发表于 2021-3-17 01:35:50 | 显示全部楼层
你这个otheruserlist不是未定义函数参数,是otheruserlist这个变量没有定义,你得先给它赋值才能在函数vlookup中传递。
回复

使用道具 举报

1

主题

4

帖子

9

积分

新手上路

Rank: 1

积分
9
发表于 2021-5-9 12:44:40 | 显示全部楼层
这个不是运行时异常。。你编译都通过不了。你使用变量之前要先声明它。
name 'otheruserlist' is not defined
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 15:01 , Processed in 0.398918 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表