首页 > 解决方案 > 有人可以解释描述输入和输出的代码以及代码的主要步骤/注释行吗

问题描述

我需要帮助理解这段代码,解释每一行中发生的事情

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()

标签: python

解决方案


让我们分解一下。

  1. 首先,导入所有必要的模块:

import pickle

  1. 定义您将使用的所有常量(在程序中永远不会改变值的变量)
LOOK_UP = 1
ADD = 2
CHANGE = 3
DELETE = 4
QUIT = 5
  1. 对于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
  1. 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

返回有效选项。

  1. 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()

推荐阅读