2016-09-16 8 views
0

私の新しいループは私の新しい販売手数料プログラムを更新していませんeveerytime私はプログラムを実行します。ここで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秒)[MSC v.1900 32ビット(インテル)] on win32 詳細については、「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 

私が間違って何をしているのですか?私はそれを把握することができません

+0

どこが間違っていると思いますか?実際には明示的ではないので、予想される入出力を書き留めるようにしてください。 – Nishant

+0

を更新していない合計を参照している場合は、各ループの開始時に0に設定しているためです。ループの外側で、total = 0を開始する直前に開始します。 – wbrugato

+0

"keep_going = True"を実行し、その後に "keep_going"を実行することをお勧めします。 "=" y "コードを使用する場合は、" keep_goingの中で '' y ''と答えてください。 – JasonD

答えて

2

あなたがループするたびにゼロに合計を設定しています。下に示すように、合計の初期化をループの外側に移動します。

0

問題は、ループを繰り返すたびに「合計」を再初期化してしまうことです。変数を初期化する必要はありませんが、必要に応じて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) 
関連する問題