Python 初学者通讯录项目

Python contact book project for beginner

如何从用户那里获取多个输入(不使用 GUI),例如姓名、phone 号码、电子邮件或 ID,并将它们存储在 python 中的变量中。我想稍后在用户搜索时向他们展示。我是初学者。

print('Contact Book')
print('''Press I to update contact
Press S to scearch''')


def updateContact():
    Name = input('Enter Your Name: ')
    MobileNo = input("Enter mobile: ")
    NameList = [].append(Name)


userinput = input("Press Key: ")
if userinput.upper().strip() == 'S':
    updateContact()

    

你需要的是一个Contactclass,里面会包含namemobileNo等属性。在 __init__ 方法(class 的构造函数)中,您可以初始化这些属性,然后在单独的方法中打印它们 showInfo.

class Contact():
    def __init__(self):  # Constructor
        self.name = input("Enter Your Name: ")
        self.mobileNo = input("Enter mobile: ")

    def showInfo(self):  # Prints info
        print("name :", self.name, ", mobile no :", self.mobileNo)


first_contact = Contact()  # New instance of the class Contact, calls __init__()
first_contact.showInfo()  # Calls showInfo() on first_contact, which prints its attributes