私有变量写入文本文件

时间:2018-12-11 06:29:11

标签: python private instance-variables

我在下面列出了主要功能和定义的功能,但是我收到一个错误,提示我没有属性“ output_info”来编写文本文件。当我尝试使用以下代码时,它给了我attribute_error。我无法将信息复制到所需的文本文件。我不知道如何将此私有实例变量调用到文本文件,以及为什么它给我一个错误,提示我我没有定义的属性。请帮忙!

定义函数(实例变量必须是私有的):     类客户:

def __init__(self,email="",last="",first="",age=0,pswd="",card="",sec=""):
    self.__email=email
    self.__last=last
    self.__first=first
    self.__age=age
    self.__pswd=pswd
    self.__card=card
    self.__sec=sec

def input_age(self):
    correct=True
    while correct == True:
        try:
            input_age=int(input("Please enter age"))
            if input_age < 0:
                print("You have entered an invalid age, age cannot be negative")
            else:
                correct=False
                self.age=input_age
        except ValueError:
            print("You have entered an invalid age, age must be a positive whole number")


def input_password(self):

    import re
    correct=True
    while correct==True :
        try:
            password=input("Please enter a password 8-12 characters with at least one upper case letter, one lower case letter and one number")
            if re.search('[A-Z]',password) is None:
                print("Password must contain at least one capital letter")
            elif re.search('[0-9]',password) is None:
                print("Password must contain at least one number.")
            elif re.search('[a-z]',password) is None:
                print("Password must contain at least one lowercase letter.")
            elif len(password)<8:
                print("Password is too short, must contain at least 8 letters")
            elif len(password)>12:
                print("Password is too long, must contain at least 8 but no more than 12 charachters")
            else:
                self.pswd=password
                correct=False
        except ValueError:
            print("Please reenter password")

def input_card_number(self):
    import re
    correct=True
    while correct==True:
        try:
            card=input("Please enter your 16 digit card number")
            if card.isdigit() == True:
                if len(card) < 16:
                    print("Invalid card number, you may have missed a digit")
                elif len(card) > 16:
                    print("Invalid card number, you may have accidentally hit a number twice.")
                else:
                    self.card=card
                    correct=False
            else:
                print("Invalid card entry, no letters in card number")
        except ValueError:
            print("Invalid card number, try again")

def input_security_code(self):
    correct=True
    while correct == True:
        try:
            sec= input("Please enter the 3 digit security code")
            if sec.isdigit()==True:
                if len(sec) < 3:
                    print("Invalid Security Code, You may have missed a digit")
                elif len(sec) > 3:
                    print("Invalid Security Code, You may have accidentally hit a digit twice")
                else:
                    self.sec=sec
                    correct=False
            else:
                print("Inv
def getInfo(self):
    return self.first,self.last,self.age,self.email,self.pswd,self.card,self.sec

def output_info(self):
    self.getInfo()
    info=(self.first," ", self.last," ", str(self.age)," ", self.email," ",self.pswd," ",self.card, " ",self.sec,"\n")
    output_file = open('customers.txt', 'a')
    output_file.write(info)
    output_file.close()

1 个答案:

答案 0 :(得分:0)

私有变量在类内不会更改,因此,如果要访问它们,则需要包括dunder(“ __”)前缀。例如:

class Customer:
    def __init__(self, email="", last="", first="", age=0, pswd="", card="",
                 sec=""):
        self.__email = email
        self.__last = last
        self.__first = first
        self.__age = age 
        self.__pswd = pswd
        self.__card = card
        self.__sec = sec 

    def output_info(self):
        output = " ".join((self.__first, self.__last, self.__age,
                           self.__email, self.__pswd, self.__card,
                           self.__sec))
        with open('customers.txt', 'a') as fhandle:
            fhandle.write(output)

可能我建议您使用一个IDE,它会自动为您填充可用的属性/方法,直到您对它们的工作方式有所了解为止。

相关问题