python - 有人可以解释描述输入和输出的代码以及代码的主要步骤/注释行吗
问题描述
我需要帮助理解这段代码,解释每一行中发生的事情
import pickle
LOOK_UP = 1
ADD = 2
CHANGE = 3
DELETE = 4
QUIT = 5
def main():
emails = {}
choice = 0
while choice != QUIT:
choice = getMenuChoice()
if choice == LOOK_UP:
lookUp(emails)
elif choice == ADD:
add(emails)
elif choice == CHANGE:
change(emails)
elif choice == DELETE:
delete(emails)
else:
exit
def getMenuChoice():
print()
print('Menu')
print('------------------------------')
print('1. Look up an email address')
print('2. Add a new name and email address')
print('3. Change an existing email address')
print('4. Delete a name and email address')
print('5. Quit the program')
print()
choice = int(input('Enter the choice: '))
while choice < LOOK_UP or choice > QUIT:
choice = int(input('Enter a valid choice: '))
return choice
def lookUp(emails):
name = input('Enter a name: ')
print(emails.get(name, 'The specified name was not found.'))
def add(emails):
name = input('Enter a name: ')
address = input('Enter an email address: ')
if name not in emails:
emails[name] = address
pickle.dump(emails, open("emails.dat", "wb"))
else:
print('That name already exists.')
def change(emails):
name = input('Enter a name: ')
if name in emails:
address = input('Enter the new address: ')
emails[name] = address
pickle.dump(emails, open("emails.dat", "wb"))
else:
print('That name is not found.')
def delete(emails):
name = input('Enter a name: ')
if name in emails:
del emails[name]
else:
print('That name is not found.')
main()
解决方案
让我们分解一下。
- 首先,导入所有必要的模块:
import pickle
- 定义您将使用的所有常量(在程序中永远不会改变值的变量):
LOOK_UP = 1
ADD = 2
CHANGE = 3
DELETE = 4
QUIT = 5
- 对于
main
函数,决定程序如何对用户将选择的 5 个选项中的每一个做出反应:
def main(): # Main function that calls the other functions
emails = {} # define an emails dict
choice = 0 # Predefine choice
while choice != QUIT:
choice = getMenuChoice()
if choice == LOOK_UP:
lookUp(emails)
elif choice == ADD:
add(emails)
elif choice == CHANGE:
change(emails)
elif choice == DELETE:
delete(emails)
else:
exit
- 在
getMenuChoice
函数中:
print()
print('Menu')
print('------------------------------')
print('1. Look up an email address')
print('2. Add a new name and email address')
print('3. Change an existing email address')
print('4. Delete a name and email address')
print('5. Quit the program')
print()
显示菜单。
choice = int(input('Enter the choice: '))
允许用户输入整数,int()
包装器将字符串转换为int
.
这里,
while choice < LOOK_UP or choice > QUIT:
程序将一直要求输入,直到用户输入一个有效的选项,在这种情况下,是 1 到 5 之间的数字,包括 1 和 5。
return choice
返回有效选项。
- 在
lookUp
函数中,
def lookUp(emails):
name = input('Enter a name: ')
print(emails.get(name, 'The specified name was not found.'))
dict.get()
如果键存在于dict
. 否则,它将返回传递到括号中的第二个参数,在本例中为'The specified name was not found.'
.
我评论了这些解释:
import pickle
LOOK_UP = 1
ADD = 2
CHANGE = 3
DELETE = 4
QUIT = 5
def main(): # Main function that calls the other functions
emails = {}
choice = 0
while choice != QUIT:
choice = getMenuChoice()
if choice == LOOK_UP:
lookUp(emails)
elif choice == ADD:
add(emails)
elif choice == CHANGE:
change(emails)
elif choice == DELETE:
delete(emails)
else:
exit
def getMenuChoice():
print()
print('Menu')
print('------------------------------')
print('1. Look up an email address')
print('2. Add a new name and email address')
print('3. Change an existing email address')
print('4. Delete a name and email address')
print('5. Quit the program')
print()
choice = int(input('Enter the choice: ')) # User inputs an integer, and the int converts the string into an int
while choice < LOOK_UP or choice > QUIT: # If the user's input is greater than 5 or smaller than 1, then ask for input again
choice = int(input('Enter a valid choice: '))
return choice # Return the valid input
def lookUp(emails):
name = input('Enter a name: ')
print(emails.get(name, 'The specified name was not found.')) # dict.get() will return the vaur for the specified key, if the key exists inside the dict, else, it will return the second pargument passed into the brackets
def add(emails):
name = input('Enter a name: ')
address = input('Enter an email address: ')
if name not in emails: # If the input name does not exist among the keys of the emails dict
emails[name] = address # Create the name as a key, and the email as the value
pickle.dump(emails, open("emails.dat", "wb")) # Write the data into emails.dat
else:
print('That name already exists.')
def change(emails):
name = input('Enter a name: ')
if name in emails:
address = input('Enter the new address: ')
emails[name] = address
pickle.dump(emails, open("emails.dat", "wb"))
else:
print('That name is not found.')
def delete(emails):
name = input('Enter a name: ')
if name in emails: # If the input name exists among the keys of the emails dict
del emails[name] # Remove it from the dict
else:
print('That name is not found.')
main()
推荐阅读
- c# - 使用 DataTable(ASP.NET MVC Web 应用程序)生成具有多个行标题的 html 表
- python - 为什么在写入 csv 文件以及添加到列表时,数字字符串和字符串文本(波斯语)的顺序会发生变化?
- botframework - 使用 Direct Line(不是 WebChat)在 LivePerson 中托管的 Microsoft Bot(使用框架 v4)未触发欢迎消息
- html - 如何在 ngfor 中设置 ngmodel
- node.js - NW.js 无法在 Sdk 和普通风味之间切换错误:rcedit.exe 失败,退出代码为 1
- python - 在heroku中部署whatsapp bot不起作用
- parallel-processing - CUDA 平铺矩阵乘法解释
- accessibility - 对于可访问的网站设计,表格还是不表格?
- flutter - Flutter GridView 在模态底部工作表中?
- selenium - Edge 的 selenium webdriver 问题,找不到文件...或 ID 为“isDisplayed.js”的嵌入式资源