Write a statement that prints the value of price in the form “X dollars and Y cents” on a line by itself.
So, if the value of price was 2345, your code would print “23 dollars and 45 cents”.
If the value was 501 it would print “5 dollars and 1 cents”. If the value was 99 your code would print “0 dollars and 99 cents”.
Solution 1:
1 2 3 4 5 6 7 8 |
centsStr=(input('Amount : ')) d, c = centsStr[:-2], centsStr[-2:] if int(centsStr) > 99: print(d + ' dollars and ' + c + ' cents') else: print('0 dollars and ' + c + ' cents') |
Solution 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def formatPrint(cents): centsStr = str(cents) d, c = centsStr[:-2], centsStr[-2:] if cents > 99: print(d + ' dollars and ' + c + ' cents') else: print('0 dollars and ' + c + ' cents') return x=int(input('Amount : ')) formatPrint(x) |
Output: