0

创建一个程序,输入工作小时数和小时费率,并输出工资。注意:每小时超过 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. "
4

2 回答 2

0

尝试使用格式方法。

print "You get paid ${0:.2f}.".format(bonus_plus_normal)
于 2013-03-22T01:39:15.213 回答
0

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

于 2013-03-22T01:40:18.880 回答