str必须是“#cents”形式,其中#是硬币的总价值

时间:2013-05-27 17:13:38

标签: python python-3.x

我打算在输入后在shell中显示“150美分” 闲置窗口中print(dollar_amount(30, 0, 0, 0, 0))。但相反,我收到此错误消息:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

我正在使用Python 3。

以下是以下问题:

def dollar_amount(nickels, dimes, quarters, loonies, toonies):
    '''(int, int, int, int, int) -> str

2 个答案:

答案 0 :(得分:2)

使用字符串格式:

dollar_amount= (nickels * 5) + (dimes * 10) + (quarters * 25) + (loonies * 100) + (toonies * 200)
return "{} cents".format(dollar_amount)

答案 1 :(得分:1)

"cents"已经是一个字符串;改变其余部分。

str(...) + " cents"