0

I'm trying to build a program that will ask how many people, create an object for each person, and take separate information from the user(ex. name, weeklyhrs, wage) for each object. That way I can simply enter information about employees and a different function can calculate salary for each one.

So far I have only been able to create two separate objects and classes for each person and would have to add each person manually to do more than one. Also as of now this only allows me to have 2 people. I would like to ask the user how many people and then have an object for each with its own individual variables/information.

class SalesPerson1:
    def __init__(self, name1 = '', weeklyhrs1 = 0, wage1 = 0):
        self.name1 = input('Enter the first employees name?')
        self.weeklyhrs1 = input('Enter the first employees number of weekly hours?')
        self.wage1 = input('Enter the first employees weekly wage?')
    def salesPerson1(self):
        return self.name1
        return self.weeklyhrs1
        return self.wage1
class SalesPerson2:
    def __init__(self, name2 = '', weeklyhrs2 = 0, wage2 = 0):
        self.name2 = input('Enter the second employees name?')
        self.weeklyhrs2 = input('Enter the second employees number of weekly hours?')
        self.wage2 = input('Enter the second employees weekly wage?')
    def salesPerson2(self):
        return self.name2
        return self.weeklyhrs2
        return self.wage2

person1 = SalesPerson1()
person2 = SalesPerson2()
nima
  • 7,796
  • 12
  • 36
  • 53
KB66
  • 3
  • 4
  • 1
    don't use input inside class and not 3 return in single function – sahasrara62 May 05 '19 at 19:26
  • Your two (or more) persons should be instances of the **same** class. I recommend you read some basic tutorial about classes, see for example [here](https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables) in the official tutorial. – Thierry Lathuille May 05 '19 at 19:32

4 Answers4

0

It looks like you need to read up on python classes. Docs are here

Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. For example (assuming the above class):

x = MyClass()

creates a new instance of the class and assigns this object to the local variable x.

Andrew Allen
  • 6,512
  • 5
  • 30
  • 73
0

just a example, but you can create a common class and use that class to process, instead of creating different classes for a different person

class SalesPerson:
    def __init__(self):
        self.name1 = []
        self.weeklyhrs1 = []
        self.wage1 =[]

    def add_name(self,name):
        self.name1.append(name)

    def add_weeklyhrs(self,weeklyhrs):
        self.weeklyhrs1.append(weeklyhrs)

    def add_wage(self,wage):
        self.wage1.append(wage)

    def show_name(self):
        return self.name1

    def show_weeklyhrs1(self):
        return self.weeklyhrs1

    def show_wage1(self):
        return self.wage1

t = int(input('no of person you want to take input : '))

x=SalesPerson()

for i in range(t):

    print("enter details for {} person".format(i+1))

    x.add_name(input('enter name '))
    x.add_weeklyhrs(input('enter hours '))
    x.add_wage(input('enter wage  '))
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0

You should create one class and use loop to run input() and to create instance of this class and add to list

class SalesPerson:

    def __init__(self, name='', weeklyhrs=0, wage=0):
        self.name = name
        self.weeklyhrs = weeklyhrs
        self.wage = wage

    def get(self):
        return self.name, self.weeklyhrs, self.wage

# list to keep all persons

all_persons = []

# get all persons

how_many = input('How many persons?')

for x in range(how_many):
    a = input('Enter the first employees name?')
    b = input('Enter the first employees number of weekly hours?')
    c = input('Enter the first employees weekly wage?')

    person = SalesPerson(a, b, c)

    all_persons.append(person)

# display all persons

for person in all_persons:
    print(person.get())
furas
  • 134,197
  • 12
  • 106
  • 148
0

Is this what you are trying to do?

  1. Let the user decide how many new instances he wants to create.
  2. Allow him to enter data for those many instances.
  3. Do whatever class operations you want to on them but only for that particular program run (i.e. not store the objects permanently in an external Excel / other databases).

The code given below will work. Please try it out.

It uses empty lists to start with and then appends them. One list is reserved for instances and the others are used for its attributes (name and age in the case given below).

The last step is just to reconfirm that it worked in the intended manner.

class People:
    def __init__(self, person, age):
        self.person = person
        self.age = age

instance = []
people = []
age = []

# Please note that this program / records added are only for 1 run of the program.
# It demonstrates how users with console input can create new instances / objects.

question = int(input("The record / list is currently empty. How many records would you like to add?     "))

for i in range(question):
    new_instance = input("Enter new person object's code     ")
    instance.append(new_instance)
    new_name = input("Enter new person's name     ")
    people.append(new_name)
    new_age = input("Enter new person's age      ")
    age.append(new_age)

print("Originally the records length was zero. The new length of records is ", len(instance))

for i in range(len(instance)):
    instance[i] = People(people[i], age[i])
    print(instance[i].person, "is", instance[i].age, "years old and is stored at the location", instance[i])

print("The last record entered was", instance[question-1].person, "whose age is", instance[question-1].age, "years.")
Ad Tel
  • 1
  • 1