Reputation: 125
My while loop isn't updating my new sales commission program eveerytime I run the program. Here is my program:
#this program calculates sales commissions and sums the total of all sales commissions the user has entered
print("Welcom to the program sales commission loop")
keep_going='y'
while keep_going=='y':
#get a salespersons sales and commission rate
sales=float(input('Enter the amount of sales'))
comm_rate=float(input('Enter commission rate'))
total=0
#calculate the commission
commission=sales*comm_rate
print("commission is",commission)
keep_going=input('Enter y for yes')
total=total+commission
print("Total is",total)
print("You have exited the program. Thet total is",total)
Here is the output of the program: Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.
Welcom to the program sales commission loop
Enter the amount of sales899
Enter commission rate.09
commission is 80.91
Enter y for yesy
Total is 80.91
Enter the amount of sales933
Enter commission rate.04
commission is 37.32
Enter y for yesy
Total is 37.32
Enter the amount of sales9909
Enter commission rate.10
commission is 990.9000000000001
Enter y for yesn
Total is 990.9000000000001
You have exited the program. Thet total is 990.9000000000001
>>>
> Blockquote
What am I doing wrong? I cannot figure it out
Upvotes: 0
Views: 9269
Reputation: 1129
The problem is that you are reinitializing 'total' every time you repeat the loop. You don't need to initialize the variable, but in case you want to, you must do it outside the while loop. The corrected code would be:
#this program calculates sales commissions and sums the total of all sales commissions the user has entered
print("Welcome to the program sales commission loop")
keep_going='y'
total=0
while keep_going=='y':
#get a salespersons sales and commission rate
sales=float(input( 'Enter the amount of sales ' ))
comm_rate=float(input( 'Enter commission rate ' ))
#calculate the commission
comission= sales * comm_rate
print( "commission is {}".format(comission) )
keep_going=input('Enter y for yes: ')
total += comission
print( "Total is {}".format(total) )
print("You have exited the program. Thet total is",total)
Upvotes: 0
Reputation: 115
Every time you loop you are setting total to zero. Move your initialization of total to outside of the loop as I show below.
#this program calculates sales commissions and sums the total of all sales commissions the user has entered
print("Welcom to the program sales commission loop")
keep_going='y'
total=0
while keep_going=='y':
#get a salespersons sales and commission rate
sales=float(input('Enter the amount of sales'))
comm_rate=float(input('Enter commission rate'))
#calculate the commission
commission=sales*comm_rate
print("commission is",commission)
keep_going=input('Enter y for yes')
total=total+commission
print("Total is",total)
print("You have exited the program. Thet total is",total)
Upvotes: 2