he final function of this problem does not require the use of either recursion or a list comprension. Rather, it will allow you to practice using conditional logic and variable assignment to gradually build up a return value.
Write a function price_string(cents) that takes as input a positive integer cents representing a price given in cents, and that constructs and returns a string in which the price is expressed as a combination of dollars and cents.
In general, the format of the returned string should be 'd dollars, c cents', where d is the whole number of dollars in the price and c is the remaining cents. For example:
>>> price_string(452) result: '4 dollars, 52 cents' >>> price_string(871) result: '8 dollars, 71 cents'
However, there are two additional rules that you must observe:
If either component of the price is 0, it should be omitted:
>>> price_string(27) result: '27 cents' >>> price_string(300) result: '3 dollars'
(Because we assume that the input cents is positive, it will never be the case that both of the components are 0!)
If either component of the price is 1, you should omit the 's' from the word for that component:
>>> price_string(201) result: '2 dollars, 1 cent' >>> price_string(117) result: '1 dollar, 17 cents' >>> price_string(101) result: '1 dollar, 1 cent'
Important Guidelines
You must begin by copying the following template into your file:
def price_string(cents): """ docstring goes here """ d = _______________ # compute whole number of dollars c = _______________ # compute remaining cents price = '' # initial value of the price string ## add code below to build up the price string return price
Begin by replacing the two blanks with expressions that will compute the values of d (the whole number of dollars) and c (the remaining cents), based on the value of the parameter cents.
Next, add code to determine the appropriate value of the variable price, which is the string that the function will return. We have given you a line assigns an empty string to price as its initial value. You should use conditional execution and string concatenation to gradually build up the final value of this variable. You will need to use assignment statements that look like this:
price = price + ...
You can use the built-in str() function to turn numbers into strings. For example, str(d) will turn the number represented by the variable d into a string.
Your function must limit itself to the single return statement that we have given you at the very end of the function. You will lose points if you use more than one return statement in this function.
def price_string(cents): """ docstring goes here """ d = cents // 100 # compute whole number of dollars c = cents % 100 # compute remaining cents price = '' # initial value of the price string if d == 1: if c == 0: price = '1 dollar' elif c == 1: price = '1 dollar, 1 cent' else: price = '1 dollar, ' + str(c) + ' cents' elif d == 0: if c == 1: price = '1 cent' elif c > 1: price = str(c) + ' cents' else: if c == 0: price = str(d) + ' dollars' elif c == 1: price = str(d) + ' dollars, 1 cent' else: price = str(d) + ' dollars, ' + str(c) + ' cents' return price print(price_string(452)) print(price_string(871)) print(price_string(27)) print(price_string(300)) print(price_string(201)) print(price_string(117)) print(price_string(101))
Get Answers For Free
Most questions answered within 1 hours.