首页 > 解决方案 > 如何将函数合并到带输入的函数中?

问题描述

我正在尝试创建一个工作通讯簿,但一直在创建主页部分。在我的代码中,我尝试使用子父继承,但它不起作用。帮助?通讯簿的任何其他提示将不胜感激。谢谢!

class Parent:
    def Create():
        create_contact = input('''Please input the details of the contact you would like to create.
                           ''')
        addressbook.append(create_contact)
        print("Successfully added")
        return
    def Remove_contact(remove_contact):
        remove_contact = input('''Please input the details of the contact you would like to remove.
                            ''')
        if 'remove_contact' in addressbook:
            print(addressbook.remove(remove_contact))
        else:
            print("Error, 'search_contact' not found in the list")
        return remove_contact
    def Search():
        search_contact = input('''Please input the details of the contact you would like to search for.
                            ''')
        if 'search_contact' in addressbook:
            print("'search_contact' found in Address Book")
        else:
            print("Error, 'search_contact' not found in the list")
        return
    def Display_contacts():
        print("Displaying contacts....", addressbook)
        return

def menu(Parent):
    menu = input('''Address book to store friends contact
   -------------------------------------------------------------
   -------------------------------------------------------------
   Select an option...
   1 - Add/Update contact...
   2 - Display all contacts...
   3 - Search...
   4 - Delete contact...
   5 - Quit
    ''')
    
    if menu == '1':
        Create()
    
    elif menu == '2':
        Display_contacts()
        
    elif menu == '3':
        Search()
        
    elif menu == '4':
        Remove_contact(remove_contact)
        
    elif menu == '5':
        print("Quitting program")
        quit()
    else:
        print("I did not understand that... Please try again")
        

menu(Parent)
        

标签: pythonfunctioninput

解决方案


这是代码。您忘记添加函数派生的类。

addressbook = []
class Parent:
    def Create():
        create_contact = input('''Please input the details of the contact you would like to create.
                           ''')
        addressbook.append(create_contact)
        print("Successfully added")
        return
    def Remove_contact(remove_contact):
        remove_contact = input('''Please input the details of the contact you would like to remove.
                            ''')
        if 'remove_contact' in addressbook:
            print(addressbook.remove(remove_contact))
        else:
            print("Error, 'search_contact' not found in the list")
        return remove_contact
    def Search():
        search_contact = input('''Please input the details of the contact you would like to search for.
                            ''')
        if 'search_contact' in addressbook:
            print("'search_contact' found in Address Book")
        else:
            print("Error, 'search_contact' not found in the list")
        return
    def Display_contacts():
        print("Displaying contacts....", addressbook)
        return

def menu(Parent):
    menu = input('''Address book to store friends contact
   -------------------------------------------------------------
   -------------------------------------------------------------
   Select an option...
   1 - Add/Update contact...
   2 - Display all contacts...
   3 - Search...
   4 - Delete contact...
   5 - Quit
    ''')
    
    if menu == '1':
        Parent.Create()
    
    elif menu == '2':
        Parent.Display_contacts()
        
    elif menu == '3':
        Parent.Search()
        
    elif menu == '4':
        Parent.Remove_contact(Parent.remove_contact)
        
    elif menu == '5':
        print("Quitting program")
        quit()
    else:
        print("I did not understand that... Please try again")

menu(Parent)

推荐阅读