Python 程序不';不要运行,只是重新启动
我刚刚写了一个程序来计算员工的工资率。对我来说,这个程序看起来很好,但是当我尝试运行它时,我重新启动了它,但它没有运行。我尝试重新启动Python GUI,但没有成功。节目如下:Python 程序不';不要运行,只是重新启动,python,restart,Python,Restart,我刚刚写了一个程序来计算员工的工资率。对我来说,这个程序看起来很好,但是当我尝试运行它时,我重新启动了它,但它没有运行。我尝试重新启动Python GUI,但没有成功。节目如下: def get_info(): hours = int(input("How many hours did you work this week?", )) while hours < 8 or hours > 86: print('Error ---- you must w
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'))
def get_info():
hours=int(输入(“您本周工作了多少小时?”,)
当小时数<8或小时数>86时:
打印('错误----您必须至少工作8小时,但不超过86小时')
小时数=整数(输入('请再次输入您的工作小时数:',))
打印()
费率=浮动(输入(“请输入您的工资费率:$”,)
当速率<7.00或速率>50.00时:
打印(“错误----付款率不能小于$7.00或大于$50.00”)
费率=浮动(输入(“请重新输入您的工资费率:$”,)
返程时间、费率
def计算小时数(num):
如果num<40:
reg_小时=num
加班费=0
其他:
注册时数=40
加班次数=num-40
返回登记时间、加班
def计算付款(num1、num2、付款率):
定期工资=num1*工资率
加班工资=num2*(工资率*1.5)
工资总额=固定工资+加班工资
返回定期工资、加班工资、总工资
def main():
获取信息()
计算小时数(小时)
工资(登记工时、加班、费率)
打印()
打印(“工资单信息”)
打印()
打印(“薪酬”,格式(薪酬,“14.2f”))
打印(“正常工作时间”,格式(reg_Hours,'10.2f'))
打印(“加班时间”,格式(加班,'10.2f'))
打印(“定期付款”,格式(定期付款,'10.2f'))
打印(“加班费”,格式(加班费,'10.2f'))
打印(“总付款”,格式(总付款,'10.2f'))
是的,图表会不稳定的。我还没能成功地运行它,使它顺利运行
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 = get_info()
reg_hours, overtime = calc_hours(hours)
regular_pay, overtime_pay, total_pay = calc_pay(reg_hours, overtime, rate)
最后,有一个打字错误calc\u pay
,而不是calu 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
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.
您可能想用
python
,variable