0

I am relatively new to Python (~5 months), so pardon my ignorance. I have been tasked with creating a program to do a few things.

First, I have a text file with information stored like the following:

name, street, city state zip, birthday, pet.

Second, my instructions include:

  • Reading the text file and determine the age and based on the age, determine a discount. For instance, if the age is 18 -25, they receive a 5% discount.
  • Match the pet preference with a vendor

I am utilizing classes for each of the attributes. I have a separate file for the class information which is indicated below. I have set/get methods for each attribute listed.

From information.py

class GetInfo:
    def __init__(self,name,street,city,state,zipcode,birthday,pet):
        self.__name = name

    def set_name(self,name):
        self.__name = name

    def get_name(self):
        return self.__name

At the end I have to write a letter for each individual. I can't figure out how to essentially keep the data separate in order to write the letter per individual. I will write each line using a .write(str) method. Here is my main program so far:

import information
import os
import time
CURRENT_YEAR = 2018

def main():
    customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
    tempFile = open('temp.txt','w')
    with open('c.TXT') as customerList:
    lines = customerList.readlines()
    for line in lines:
        tempFile.write( str(GetInfo.name) + '\n')
    tempFile.close()
main()

For the vendor list I will have a dictionary such as:

vendors = {"Cat:CatCity"}

I am perplexed at my next step, while also receiving this error when trying to run the program:

"main.py", line 10, in main customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
NameError: name 'name' is not defined
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • 5
    `set_name` and `get_name` are totally useless function. Just use normal attribute access. Python != Java. Also, don't use double-underscore name-mangling unless you *want* double-underscore name-mangling. Which you dont – juanpa.arrivillaga Nov 12 '18 at 19:07
  • 1
    Anyway, your error is pretty straightforward, `name` is not defined anywhere. – juanpa.arrivillaga Nov 12 '18 at 19:08
  • Thanks for your reply. Do I need to add name = def get_name() in the main program? – iStatiK Nov 12 '18 at 19:09
  • 2
    no, `name = def get_name()` is a SyntaxError. Anyway, your `get_name` functions are not needed, and I don't see what you expect them to do in that case – juanpa.arrivillaga Nov 12 '18 at 19:11
  • He's trying to tell you, that instead of doing `customerInfo.get_name()` you can just do `customerInfo.__name` and it will return the same thing. In your case, when you set up `GetInfo(name, ...)` the variable `name` is never defined **before** you use the variable. Class/Instance variables are not private or hidden from other scopes (like in most/some other languages). Here, you can just access the attributes straight away without having to go through any functions or constructors. – Torxed Nov 12 '18 at 19:12
  • 2
    @Torxed actually, that *will not*, since they used double-underscore name-mangling. But they *shouldn't have*. So, it should just be `.name` all the way through – juanpa.arrivillaga Nov 12 '18 at 19:13
  • If i dont use those functions, how do I assign the variables(name, pet,etc.) so i can write them in the end? – iStatiK Nov 12 '18 at 19:13
  • 1
    That question doesn't make any sense. You don't need those functions to assign to variables. Indeed, those functions don't do that at all, they assign to instance attributes. But you don't need them to do that either. – juanpa.arrivillaga Nov 12 '18 at 19:14
  • Ah right, f**. You're 100% correct @juanpa.arrivillaga. Forgot about those magic keywords.. – Torxed Nov 12 '18 at 19:15
  • 1
    @Torxed this is clearly another case of someone learning Python in Java... it's a constant struggle on the Python tag – juanpa.arrivillaga Nov 12 '18 at 19:15
  • 1
    @juanpa.arrivillaga Yea, been here a few years. I get this quite a lot too, just forgot about it for a second. It's either this or questions concerning how to work/modify dictionaries and/or lack of stack traces. Glad you reminded me of the double underscores, I rarely ever work with them :P – Torxed Nov 12 '18 at 19:17
  • @juanpa.arrivillaga. Good work helping wean OP to python. Can't wait for them to discover properties :) – Mad Physicist Nov 12 '18 at 19:23
  • Like i stated, I am very new to Python, so I apologize. I have only used IDLE to write anything thus far. I guess my question would then be, how do I tell Python to use the first line of the text file I opened, that the first increment is the name? i.e. text file is jane doe,street, city, st... I have multiple lines with different people and they have to stay separate. – iStatiK Nov 12 '18 at 19:23

1 Answers1

-1

Try this class:

class GetInfo:
    def __init__(self,name,street,city,state,zipcode,birthday,pet):
        self.inner_name = name

    @property
    def get_name(self):
        return self.inner_name

    @name.setter
    def set_name(self, name):
        self.inner_name = name

You do not need to use the getter and the setter. In python you can access all attributes of an object without them. See also: Using @property versus getters and setters

The actual problem in your Code is the line

tempFile.write( str(GetInfo.name) + '\n')

The object GetInfo is not an instance of the class, but the class itself. And the class does not have the attribute name. What you probably meant was:

tempFile.write( str(customerInfo.name) + '\n')
ostcar
  • 161
  • 4
  • 1
    remove the totally useless `property` methods – juanpa.arrivillaga Nov 12 '18 at 19:14
  • Also, the issue isn't an AttributeError, it's a NameError, because when they initialize their `GetInfo` instance, they pass a bunch of variables that aren't defined (`name` being only the first one) – juanpa.arrivillaga Nov 12 '18 at 19:16
  • Thank you for your suggestion. I havent changed the class yet, but I did try changing it to customerInfo and received the same error. Do I need to assign name to something in the class file or main program? – iStatiK Nov 12 '18 at 19:25