[英]Sales commission program using while loop. Value not updating
我的while循环没有在我运行程序时更新我的新销售佣金程序。 这是我的程序:
#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)
程序的输出如下:Python 3.5.2(v3.5.2:4def2a2901a5,2016年6月25日,22:01:18)在win32上的[MSC v.1900 32位(Intel)]键入“ copyright”,“ credits”或“ license()”以获取更多信息。
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
我究竟做错了什么? 我想不明白
每次循环时,您都将total设置为零。 如下所示,将total的初始化移到循环外部。
#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)
问题在于,每次重复循环时,您都要重新初始化“总计”。 您无需初始化变量,但是如果需要,则必须在while循环之外进行。 更正后的代码将是:
#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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.