user2425814
user2425814

Reputation: 409

The str must be of the form "# cents" where # is the total value of the coins

I'm trying to get "150 cents" to be displayed in shell after I type in print(dollar_amount(30, 0, 0, 0, 0)) in the idle window. But instead, I get this error message:

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

I'm using Python 3.

Here is the question below:

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

Upvotes: 0

Views: 67

Answers (2)

Jon Clements
Jon Clements

Reputation: 142166

Use string formatting:

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

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798726

"cents" is already a string; change the rest.

str(...) + " cents"

Upvotes: 1

Related Questions