从main()

时间:2019-04-29 19:48:49

标签: python oop

我正在尝试从main函数设置一个实例变量。在客户类中,我创建了 init ()。 self .__ email =电子邮件 在主要功能中,我试图将两个独立对象的电子邮件设置为等于主要功能中的输入。 我已经在main中初始化了对象,但似乎似乎找不到/弄清楚如何为main函数中建立的2个单独对象创建2个单独的电子邮件。

from customer import Customer
def main():

    customer1 = Customer('firstname', 'lastname', 'email', 'password', 'card_number', 'security_code')
    customer2 = Customer('firstname', 'lastname', 'email', 'password', 'card_number', 'security_code')
    customer1.email = input("Enter email address:") # trying to set email in __init__ (for class Customer)for customer1 equal to the input
    customer1.input_info()
    customer1.verify_info()
    customer2.email = input("Enter email address:")
    customer2.input_info()
    customer2.verify_info()
    file1 = open("customers.txt", "a")
    file1.write(customer1.output_info())
    file1.write(customer2.output_info())

main()
class Customer:
def __init__(self, firstname, lastname, email, password, card_number, security_code):
    self.__firstname = firstname
    self.__lastname = lastname
    self.__age = 0
    self.__email = email
    self.__password = password
    self.__card_number = card_number
    self.__security_code = security_code

结果应将customer1和customer2的电子邮件设置为等于主功能中的输入。然后将其写入文本文件。 实际结果是在文本文件中打印“电子邮件”,而不是输入。

1 个答案:

答案 0 :(得分:2)

尝试使用setattr。示例:

mail = input("Enter email address:")
setattr(customer1, 'email', mail)