问题遇到的现象和发生背景
问题一:lambda函数究竟是什么原理?
问题二:请看代码
def switch(option):
funcdic={
1:lambda:searchMode(),
2:lambda:editMode(),
3:lambda:exit()
}
return funcdic[option]()
这一段代码 假如我在查询单词里我输入了数字9(代表退出查询,回到主菜单),那么请问return了什么使得这一段函数能成功跳出并执行下一段代码(即:怎么又回到了主菜单)
while True:
print('----------欢迎实用英汉词典---------')
print('1.查询单词\n2.添加单词\n3.退出\n')
option=int(input('请输入你的选择:'))
switch(option)
问题相关代码,请勿粘贴截图
```python
dict={}
digits='0123456789'
path='F:\\Users\\马欣桐\\Documents\\python\\本书资源\\字典\\dictionary.txt' #最后要把输入键放到下一行
def readFile(path,arg):
try:
file=open(path,arg,encoding='GBK') #格式需要ANSI的
except:
file=open(path,'w',encoding='GBK')
return file
def readWords():
file=readFile(path,'r')
while True:
line=file.readline()
#print(line)
if not line:
break
word=line.split(' ',2)
#print(word)
dict[word[0]]=word[1][:-1]
#print(dict)
file.close()
def writeFile(word,dsp):
file=readFile(path,'a')
file.write('{} {}\n'.format(word,dsp))
file.close()
def modifyFile(word,dsp):
file=readFile(path,'r')
line=file.readlines()
#print(line)
flen=len(line)-1
#print(flen)
for i in range(flen):
if word in line[i]:
file.close()
line[i]='{} {}\n'.format(word,dsp)
print(line[i])
file=readFile(path,'w')
print(line)
file.writelines(line)
break
file.close()
def editMode():
print('*'*50)
print('*'*50)
while True:
word=input('(按数字键退出)请输入你想添加或修改的单词:')
if word in digits:
print('*'*50)
print('*'*50)
return
try:
print('该单词已存在于单词库,当前解释是:{}'.format(dict[word]))
except:
print('您添加的是一个新单词')
print('----------------------------------')
description=input('请输入您的解释:\n')
try:
dict[word]+=',%s'%description
modifyFile(word,dict[word])
except KeyError:
dict[word]='%s'%description
writeFile(word,dict[word])
print('------------添加完成--------------')
def searchMode():
print('*'*50)
print('*'*50)
while True:
word=input("(按数字键退出)请输入你想查的单词:")
if word in digits:
print('*'*50)
print('*'*50)
return
print('----------------------------------')
try:
print(dict[word])
except KeyError:
print('对不起,这个单词没有收录')
print('----------------------------------')
def interface():
readWords()
def switch(option):
funcdic={
1:lambda:searchMode(),
2:lambda:editMode(),
3:lambda:exit()
}
return funcdic[option]()
while True:
print('----------欢迎实用英汉词典---------')
print('1.查询单词\n2.添加单词\n3.退出\n')
option=int(input('请输入你的选择:'))
switch(option)
interface()
###### 运行结果及报错内容