调用函数时,如何捕获因未设置模块变量而导致的错误
所以我有这个代码
file = "whatever the file path is"
menu= [] #this is a global variable
def ordermenu():
with open(file) as f: # read file
reader = csv.reader(f, delimiter=",")
next(reader, None) #skip the header
def showmenu():
for i in range(len(menu)):
print(menu)
ordermenu()
showmenu()
在这个函数中,我需要捕捉错误
注意:我不会使用这些全局变量作为参数
它被称为
NameError
,用于表示未定义的内容。只需用try
-except
像这样包装代码:相关问题 更多 >
编程相关推荐