2016-09-16 2 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) 

Вот вывод программы: Python 3.5.2 (v3.5.2: 4def2a2901a5, 25 Июнь 2016, 22:01:18) [MSC v.1900 32 бит (Intel)] on win32 Тип «авторское право», «кредиты» или «лицензия()» для получения дополнительной информации.

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», а затем «while keep_going». Если вы хотите использовать код «=», тогда вы должны сказать «while» y в keep_going. – JasonD

ответ

2

Каждый раз, когда вы зацикливаете, вы устанавливаете значение 0. Перенесите свою инициализацию итогового значения за пределы цикла, как показано ниже.

#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) 
Смежные вопросы