2012-04-06 2 views
0

Я только что написал программу для расчета ставок оплаты труда. Для меня программа выглядит просто отлично, но когда я пытаюсь запустить ее, я получаю перезагрузку, и она не запускается. Я попытался перезапустить графический интерфейс Python, но не повезло. Вот программа:Программа не запускается, просто перезагружается

def get_info(): 
    hours = int(input("How many hours did you work this week?",)) 
    while hours < 8 or hours > 86: 
     print('Error ---- you must work at least 8 hours and no more than 86 hours') 
     hours = int(input('Please enter your hours worked again:',)) 
    print() 
    rate = float(input("Please enter your pay rate: $",)) 
    while rate < 7.00 or rate > 50.00: 
     print("Error ---- pay rate cannot be less than $7.00 or greater than $50.00") 
     rate = float(input("Please re-enter your pay rate: $",)) 

    return hours, rate 

def calc_hours(num): 
    if num < 40: 
     reg_hours = num 
     overtime = 0 
    else: 
     reg_hours = 40 
     overtime = num - 40 

    return reg_hours, overtime 

def calc_pay(num1, num2, pay_rate): 
    regular_pay = num1 * pay_rate 
    overtime_pay = num2 * (pay_rate * 1.5) 
    total_pay = regular_pay + overtime_pay 

    return regular_pay, overtime_pay, total_pay 

def main(): 
    get_info() 
    calc_hours(hours) 
    cal_pay(reg_hours, overtime, rate) 

    print() 
    print ("      Payroll Information") 
    print() 
    print ("Pay Rate", format(rate, '14.2f')) 
    print ("Regular Hours", format(reg_hours, '10.2f')) 
    print ("Overtime Hours", format(overtime, '10.2f')) 
    print ("Regular Pay", format(regular_pay, '10.2f')) 
    print ("Overtime Pay", format(overtime_pay, '10.2f')) 
    print ("Total Pay", format(total_pay, '10.2f')) 

Да, график будет неуклюжим. Я не смог запустить его успешно, чтобы он прошел гладко.

+0

Вы можете помечать свой вопрос с 'python',' variable' – George

ответ

1
hours, rate = get_info() 
reg_hours, overtime = calc_hours(hours) 
regular_pay, overtime_pay, total_pay = calc_pay(reg_hours, overtime, rate) 

print() 
print ("      Payroll Information") 
print() 
print ("Pay Rate", format(rate, '14.2f')) 
print ("Regular Hours", format(reg_hours, '10.2f')) 
print ("Overtime Hours", format(overtime, '10.2f')) 
print ("Regular Pay", format(regular_pay, '10.2f')) 
print ("Overtime Pay", format(overtime_pay, '10.2f')) 
print ("Total Pay", format(total_pay, '10.2f')) 

Во-первых, взгляните на ваш main():. Вы вызвали свою функцию get_info(), когда функция закончила, она возвращает hours, rate, но вы не сохранили результат. (который является вашим hours, rate), а также следующие две строки. Когда вы вызываете свои методы, они возвращают ответы, вы должны хранить их в переменной.

Эти 3 линии изменения

hours, rate = get_info() 
reg_hours, overtime = calc_hours(hours) 
regular_pay, overtime_pay, total_pay = calc_pay(reg_hours, overtime, rate) 

Наконец, есть опечатка calc_pay вместо cal_pay того, что вы пишете. Так что фиксация, которая заставит вашу программу работать, вот вывод.

Выход

How many hours did you work this week?8 

Please enter your pay rate: $20 

        Payroll Information 

Pay Rate   20.00 
Regular Hours  8.00 
Overtime Hours  0.00 
Regular Pay  160.00 
Overtime Pay  0.00 
Total Pay  160.00 

И позвольте мне объяснить вам, что это оператор присваивания сделал. Форма такова: variable = expression

  1. выражение на RHS проводится оценка (значение представляет собой адрес памяти)
  2. Сохранение адреса памяти в переменной на LHS

A ссылка может быть полезна для чтения: Defining Functions

Если вы хотите исправить свой чат, вот как это сделать.

pattern = '{0:15s} {1:4.2f}' 
print(pattern.format('Pay Rate', rate)) 
print(pattern.format('Regular Hours', reg_hours)) 
print(pattern.format('Overtime Hours', overtime)) 
print(pattern.format('Regular Pay', regular_pay)) 
print(pattern.format('Overtime Pay', overtime_pay)) 
print(pattern.format('Total Pay', total_pay)) 

Выход:

Pay Rate   20.00 
Regular Hours  20.00 
Overtime Hours  0.00 
Regular Pay  400.00 
Overtime Pay  0.00 
Total Pay   400.00 

Объяснение:

pattern = '{0:15s} {1:4.2f}' 
# 0 mean the blank should be filled with the first argument, 
# the colon(:) specifies the formatting of the string/number. 
# s means to format a string, 15s means the string will be padded with spaces 
# so it will take up exactly 15 spaces, without the number, s just mean 
# use the string without any space padding 
# d means format an integer, 4d mean the integer will be padded with space 
# so it takes up exactly 4 spaces. f means float, and .2 mean 2 decimal point. 
Смежные вопросы