Menu

[solved]-Write Function Pricestring Cents Takes Positive Integer Cents Input Cents Price Given Cent Q39028748

Write function price_string(cents) that takes a positiveinteger, cents, as input. Cents = price given in cents and returnsa string where price is expressed as a combo of dollars and cents.The format of the returned string should be ‘d dollars, c cents.’*Done in Python 3.7; we have NOT LEARNED FOR/WHILE LOOPS YET. Weonly know recursion, list comprehension and if clauses.

Ex: >>> price_string(452) –> ‘4 dollars, 52 cents’|| >>> price_string(871) –> ‘8 dollars, 71 cents’

*IMPORTANT GUIDELINES – MUST INCLUDE:

1.) If either component of price == 0, it should be omitted:

Ex. >>> price_string(27) –> ’27 cents’ ||>>> price_string(300) –> ‘3 dollars’

2.) If either component of price == 1, omit ‘s’ from theword:

Ex. >>> price_string(201) –> ‘2 dollars, 1 cent’ ||>>> price_string(117) –> ‘1 dollar, 17 cents’

3.) Code must follow this template:

def price_string(cents):

d = ____ # compute whole # of dollars

c = ____ # compute remaining cents

price = ” # initial value of price string

## add code below to build up price string

return price

So far I have a code that works 100%, but it has more than onereturn statement and does not use ‘price’ at all, therefore it iswrong. Can anyone help me?

Expert Answer


Answer to Write function price_string(cents) that takes a positive integer, cents, as input. Cents = price given in cents and retu… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *