2016-09-16 4 views
0

내 while 루프가 새 판매 수수료 프로그램을 업데이트하지 않습니다. 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) 

프로그램의 출력은 다음과 같습니다 : 여기 내 프로그램입니다 파이썬 3.5.2 (V3.5.2 : 4def2a2901a5 이세 (25) 2016 22시 1분 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으로 설정된다. 아래에 표시된대로 루프의 외부로 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) 
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)