使用while循环的销售佣金计划。价值未更新

时间:2016-09-16 17:29:11

标签: python while-loop updating

我的while循环没有更新我的新销售佣金程序eveerytime我运行程序。这是我的计划:

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

这是程序的输出:win32上的Python 3.5.2(v3.5.2:4def2a2901a5,2016年6月25日,22:01:18)[MSC v.1900 32位(英特尔)]     输入“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

我做错了什么?我无法弄清楚

2 个答案:

答案 0 :(得分:2)

每次循环时,您将总计设置为零。如下所示,将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)

答案 1 :(得分:0)

问题是你要重新初始化'总计'每次重复循环。您不需要初始化变量,但是如果您愿意,您必须在while循环之外执行此操作。更正后的代码为:

<div class="project-nav" style="display:none;">
	<div class="nav-container">	

		<?php
		$catPost = get_posts('cat=5&posts_per_page=-1000');
		foreach ($catPost as $post) : setup_postdata($post); ?>

			<a href="<?php the_permalink(); ?>">
				<div class="nav-fixed">
					<p><?php the_title(); ?></p>
				</div>
			</a>

		<?php  endforeach;?>
	</div>
</div>