【发布时间】: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() 调用。另外我不认为继承在这里是一个好方法,我会将所有这些嵌入到一个类中并定义一个提示用户并相应地调用正确函数的方法。