简体   繁体   English

如何将函数合并到带输入的函数中?

[英]How do I incorporate functions into functions with inputs?

I'm attempting to create a working address book and am stuck on creating the home page portion.我正在尝试创建一个工作地址簿,但一直在创建主页部分。 In my code I try to use the child - parent inheritance yet it does not work.在我的代码中,我尝试使用 child-parent 继承,但它不起作用。 Help?帮助? Any other tips for the address book would be apprecciated.对于地址簿的任何其他提示将不胜感激。 Thanks!谢谢!

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)
        

Here is the code.这是代码。 You forgot to add the class from which the functions derive.您忘记添加函数派生自的类。

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)

If your purpose is to use class inheritance, then I think the following is what you are looking for:如果您的目的是使用类继承,那么我认为以下是您要查找的内容:

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

class Menu(Parent):
    def run(self):
        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':
            self.create()

        elif menu == '2':
            self.display_contacts()

        elif menu == '3':
            self.search()

        elif menu == '4':
            self.remove_contact(remove_contact)

        elif menu == '5':
            print("Quitting program")
            quit()
        else:
            print("I did not understand that... Please try again")
            
new_menu = Menu()

Then whenever you want to call the menu:然后每当你想调用菜单时:

new_menu.run()

As mentioned before, it is not apparent that you need inheritance.如前所述,您需要继承并不明显。 Create an instance of Parent and reference it like this.创建一个 Parent 的实例并像这样引用它。

p = Parent()

You can then pass the instance p to menu.然后您可以将实例 p 传递给菜单。

menu(p)

You have to make a few changes to your class and method as well.您还必须对类和方法进行一些更改。

addressbook = []
class Parent:
    def Create(self):
        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(self, 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(self):
        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(self):
        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(remove_contact)

    elif menu == '5':
        print("Quitting program")
        parent.quit()
    else:
        print("I did not understand that... Please try again")

p = Parent()
menu(p)

This will get your program a little further along... It might make sense to make addressbook an attribute of your Parent class which maybe should be called AddressBook.这将使您的程序更进一步......让地址簿成为您的父类的属性可能应该被称为地址簿。

Also, read about python classes https://docs.python.org/3/tutorial/classes.html另外,阅读有关 python 类的信息https://docs.python.org/3/tutorial/classes.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM