需要使代码全部模块化。目标是根据用户输入计算毛额和奖金

时间:2016-09-11 13:44:35

标签: python python-3.x module global-variables

#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()

1 个答案:

答案 0 :(得分:0)

应该是你想要的。试试这个

#global variable
CONTRIBUTION_RATE = 0.05

def main():
    gross = GetGrossPay()
    bonus = GetBonusPay()
    sum = GetSumContribution(gross, bonus)
    showGrossPayContrib(gross)
    showBonusContrib(bonus)
    showSumContrib(sum)


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 GetSumContribution(gross, bonus):
    return sum((gross*CONTRIBUTION_RATE, bonus*CONTRIBUTION_RATE))

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)

def showSumContrib(sumContrib):
    print("The sum of the Bonus and Gross contributions is ${}".format(sumContrib))

main()