Alina Alina - 2 months ago 9
Python Question

Need to make code all modular. Goal is to calculate gross and bonus pay contributions based on user input

global variable



CONTRIBUTION_RATE = 0.05

def main():
#if these are set to 0 they do not calculate contribution but it does run the program.
grossPay = 0
bonusPay = 0

#gets gross pay from input
GetGrossPay(grossPay)

#gets bonus pay from input
GetBonusPay(bonusPay)

#takes input from GetGrossPay to calculate contrib
showGrossPayContrib(grossPay)

#takes input from GetBonusPay to calculate contrib
showBonusContrib(bonusPay)

#This will prompt user to enter gross pay


def GetGrossPay(grossPay):
grossPay = float(input("Enter the total gross pay: "))
return grossPay

#This will prompt user to enter bonus pay


def GetBonusPay(bonusPay):
bonusPay = float(input("Enter the total bonus pay: "))
return bonusPay

#This SHOULD take the grossPay from GetGrossPay module to get GrossPayContrib


def showGrossPayContrib(theGrossPay):
theGrossPay = CONTRIBUTION_RATE * theGrossPay
print("The contribution for the gross pay is $ ",theGrossPay)

#This SHOULD take the bonusPay from GetBonusPay module to get BonusContrib


def showBonusContrib(theBonus):
theBonus = CONTRIBUTION_RATE * theBonus
print("The contribution for the bonuses is $ ",theBonus)

main()

Answer

The issue is that you never overwrite the value of grosspay and bonuspay. Here's how you do that!

#global variable
CONTRIBUTION_RATE = 0.05

def main():
    showGrossPayContrib(GetGrossPay())#no reason to pre-define either bonus or gross
    showBonusContrib(GetBonusPay())

def GetGrossPay():
    #as it stands there is no reason to pass a variable in
    return float(input("Enter the total gross pay: "))

def GetBonusPay():
    return float(input("Enter the total bonus pay: "))

def showGrossPayContrib(theGrossPay):
    theGrossPay = CONTRIBUTION_RATE * theGrossPay
    print("The contribution for the gross pay is $ ",theGrossPay)

def showBonusContrib(theBonus):
    theBonus = CONTRIBUTION_RATE * theBonus
    print("The contribution for the bonuses is $ ",theBonus)

main()
Comments