Python代码TypeError需要帮助请

时间:2013-03-22 01:33:32

标签: python

创建一个程序,输入工作小时数和小时费率,并输出工资。注意:每小时超过40小时,它们就会获得1.5倍的资金。

错误在第22行和第25行。我想将答案舍入到2位小数,但它说“并非在字符串格式化期间转换所有参数。”

# Constants for hours over 40
BONUS_MONEY_HOURS=40.0
BONUS_MONEY_RATE=1.5

# Inputting the hourly rate and amount of hours worked
hourly_rate=float(input("Please enter how much you make per hour: "))
hours_worked=float(input("Please enter how many hours worked: "))

# Formulas for calculating the amount paid
hours_under_40=float(hourly_rate*hours_worked)
hours_over_40=float(hours_worked-BONUS_MONEY_HOURS)
bonus_money=float(hours_over_40*BONUS_MONEY_RATE)
bonus_plus_normal=float(bonus_money+hours_under_40)

# Outputting the amount paid from different inputs
if hours_worked > 0 and hours_worked < BONUS_MONEY_HOURS:
    print "You get paid $.2f"%hours_under_40

elif hours_worked > 0 and hours_worked > BONUS_MONEY_HOURS:
    print "You get paid $.2f"%bonus_plus_normal

elif hours_worked < 0:
    print "Invalid input. "

elif hourly_rate < 0:
    print "Invalid input. "

2 个答案:

答案 0 :(得分:0)

尝试使用format方法。

print "You get paid ${0:.2f}.".format(bonus_plus_normal)

答案 1 :(得分:0)

尝试$%。2f而不是$ .2f你需要%,否则它不是格式化字符。此外,看起来你的程序无法处理有人工作40小时的情况,这是很常见的。 :)