Home > Software engineering >  Trying to display correct values of same variable?
Trying to display correct values of same variable?

Time:03-17

I need to display sum of all commission rather than outputting the commission of last entered sales. How can I display the user's sum of all sales commission. Where can I fix this? Any feedback is appreciated!

def main():
    sales_amount = 0.0
    total_sales_amount = 0.0
    commission = 0.0
    more_sales = 'Y'

    while more_sales == 'Y':
        sales_amount = get_sales_amount()
        total_sales_amount = total_sales_amount   (sales_amount   sales_amount)
        more_sales = more_sales_input()

    commission = get_commission_calc(sales_amount)
    print_totals(commission)

def more_sales_input():
    more = ""
    more = input("Do you have more sales to add? (y/n): ")
    more = more.upper()
    while more != "Y" and more!= "N":
        print("Invalid entry.")
        more = input("Do you want to add more items: ")
        more = more.upper()
    return more

def get_sales_amount():
    sales = 0.0
    sales = float(input("\nPlease enter sales $ "))
    return sales

def get_commission_calc(sales):
    commission = 0.0
    if sales >= 20000:
        commission = sales * .10
    elif sales >= 10000:
        commission = sales * .07
    else:
        commission = sales * .05
    return commission

def print_totals(total_commission):
    print("\nYour commission is", '${:,.2f}'.format(total_commission))

main()

CodePudding user response:

your commission variable is outside the while loop, it make sense to only print the last one because you are setting the variable after the loop finish and the value you are setting will always be the last value of sales_amount since you are changing it on every iteration.

to resolve, store the sum of all the sales in commission variable:

commission  = get_commission_calc(sales_amount)

this should be inside the while loop and remove the other calculation. your main function will look like:

def main():
    sales_amount = 0.0
    total_sales_amount = 0.0
    commission = 0.0
    more_sales = 'Y'
    
    while more_sales == 'Y':
        sales_amount = get_sales_amount()
        total_sales_amount = total_sales_amount   (sales_amount   sales_amount)
        more_sales = more_sales_input()
        commission  = get_commission_calc(sales_amount)
    print_totals(commission)

x = y is equal to x = x y

  • Related