【问题标题】:How do I incorporate functions into functions with inputs?如何将函数合并到带输入的函数中?
【发布时间】:2021-07-18 23:40:23
【问题描述】:

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

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)
        

【问题讨论】:

  • 但究竟是什么问题?
  • 当我运行代码时它只是在输入后停止,它不会运行其他函数。
  • 子类必须是类。您只是将 作为参数传递给 menu()
  • 会是这样吗:class Child(Parent): menu(Parent)
  • 您必须将 Parent 的实例,即 Parent() 传递给 menu() 调用。另外我不认为继承在这里是一个好方法,我会将所有这些嵌入到一个类中并定义一个提示用户并相应地调用正确函数的方法。

标签: python function input


【解决方案1】:

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

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)

【讨论】:

    【解决方案2】:

    如果您的目的是使用类继承,那么我认为您正在寻找以下内容:

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

    然后每当你想调用菜单时:

    new_menu.run()
    

    【讨论】:

      【解决方案3】:

      如前所述,您需要继承并不明显。创建一个 Parent 实例并像这样引用它。

      p = Parent()
      

      然后您可以将实例 p 传递给菜单。

      menu(p)
      

      您还必须对您的类和方法进行一些更改。

      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)
      

      这将使您的程序更进一步......将地址簿作为父类的属性可能应该称为地址簿。

      另外,阅读 python 类https://docs.python.org/3/tutorial/classes.html

      【讨论】:

        猜你喜欢
        • 2014-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-17
        相关资源
        最近更新 更多