Home > Blockchain >  Repeating Loop Inside of Classes
Repeating Loop Inside of Classes

Time:09-22

I am creating a program that asks for the user's name, number, hourly pay rate, and shift, and stores all the values inside of two classes and print them again.

Here is my code:

class Employee(): # Set class that keeps data attributes of name & number

    def __init__(self):
        name = input("Name?: ")
        self.name = name
        
        number = input("Number?: ")
        self.number = number
        
        ProductionWorker.Shift(self)
        
class ProductionWorker(Employee):
              
    def Pay(self):
        pay = input("Pay?: ")
        self.pay = pay
        
    def Shift(self):
        shift = input("Shift?: ")
        self.shift = shift
        
        if shift == 1:
            self.shift = "day"
            ProductionWorker.Pay(self)
        elif shift == 2:
            self.shift = "night"
            ProductionWorker.Pay(self)
    

def result():  # Print results
    userName = Employee()
    userNumber = Employee()
    userShift = ProductionWorker()
    userPay = ProductionWorker()

    print(userName.name)
    print(userNumber.number)
    print(userShift.shift)
    print("$"   userPay.pay)
    
    quit()
    
result()  

What is the reason that this code causes a repeating loop when initiated?

This is the result:

Name?: John
Number?: 1234567 
Shift?: 1
Name?: John..
Number?: 1234567
Shift?: again?
Name?: 

CodePudding user response:

Here it is I caught were you have a mistake Problem You are makeing class objects 4 times

Here, def result(): # Print results

userName = Employee()
userNumber = Employee()
userShift = ProductionWorker()
userPay = ProductionWorker()

As ProductionWorker class is inherites Employees class it will run it init function Thats why its asking you for inputs for four times(I have checked that)

And when you are checking your shift input while it's 1(day) or 2(night)

if shift == 1:
    self.shift = "day"
    ProductionWorker.Pay(self)
elif shift == 2:
    self.shift = "night"
    ProductionWorker.Pay(self)

It is throughing error

Solution You should make a class object only ones For Example

user=ProductionWorker()

We are making object of production class because it is inherites employee class in it. So will be able to do all functions of employee class from it.

Solution2

You are taking shift as input and input is by default string. And you are comparing it with integer. So either you have to convert shift to int or compare it with int.

if shift == "1":
    self.shift = "day"
    ProductionWorker.Pay(self)
elif shift == "2":
    self.shift = "night"
    ProductionWorker.Pay(self)

Like Above

Final Code

class Employee(): # Set class that keeps data attributes of name & number

    def __init__(self):
        name = input("Name?: ")
        self.name = name
        
        number = input("Number?: ")
        self.number = number
        
        ProductionWorker.Shift(self)
        
class ProductionWorker(Employee):
              
    def Pay(self):
        pay = input("Pay?: ")
        self.pay = pay
        
    def Shift(self):
        shift = input("Shift?: ")
        self.shift = shift
        
        if shift == "1":
            self.shift = "day"
            ProductionWorker.Pay(self)
        elif shift == "2":
            self.shift = "night"
            ProductionWorker.Pay(self)
    

def result():  # Print results
    user = ProductionWorker()

    print(user.name)
    print(user.number)
    print(user.shift)
    print("$"   user.pay)
    
    quit()
    
result()  

CodePudding user response:

So there's a lot going on here that I see.

Here's an example for something specific that I see.

class Federal:

    def __init__(self, bracket, taxrate):
        self.bracket = int(bracket)
        self.taxrate = float(taxrate)

# FEDERAL
fbracket1 = Federal(1, .10)
fbracket2 = Federal(2, .12)
fbracket3 = Federal(3, .22)
fbracket4 = Federal(4, .24)
fbracket5 = Federal(5, .32)
fbracket6 = Federal(6, .35)
fbracket7 = Federal(7, .37)


class Withholdings:

    def __init__(self, taxrate):
        self.taxrate = float(taxrate)


# WITHHOLDINGS
social_security = Withholdings(0.062)
medicare = Withholdings(0.0145)

When you call def init method(we call those dunders, like double unders), you need to pass in the objects attributes. Like in my Federal class, I called def init with bracket and taxrate object attributes. Below that you can see variables I created related to the bracket number and taxrates.

I would suggest looking up Corey Schaffer on YT. He has a great playlist on Python functions and classes and he does a great job defining them clearly.

  • Related