Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 4 - Selections - Programming Exercises - Page 122: 4.7

Answer

Code

Work Step by Step

# (Financial application: monetary units) Modify Listing 3.4+ ComputeChange.py+ # to display the nonzero denominations only+ using singular words for single units # such as 1 dollar and 1 penny+ and plural words for more than one unit such as 2 # dollars and 3 pennies. amount = eval(input("Enter an amount in double+ e.g.+ 11.56: ")) remainingAmount = int(amount * 100) numberOfOneDollars = int(remainingAmount / 100) remainingAmount = int(remainingAmount % 100) numberOfQuarters = int(remainingAmount / 25) remainingAmount = remainingAmount % 25 numberOfDimes = int(remainingAmount / 10) remainingAmount = remainingAmount % 10 numberOfNickels = int(remainingAmount / 5) remainingAmount = remainingAmount % 5 numberOfPennies = remainingAmount s = "Your amount " + str(amount) + " consists of\n" if numberOfOneDollars == 1: s += "\t " + str(numberOfOneDollars) + " dollar\n" elif numberOfOneDollars > 1: s += "\t " + str(numberOfOneDollars) + " dollars\n" if numberOfQuarters == 1: s += "\t " + str(numberOfQuarters) + " quarter\n" elif numberOfQuarters > 1: s += "\t " + str(numberOfQuarters) + " quarters\n" if numberOfDimes == 1: s += "\t " + str(numberOfDimes) + " dime\n" elif numberOfDimes > 1: s += "\t " + str(numberOfDimes) + " dimes\n" if numberOfNickels == 1: s += "\t " + str(numberOfNickels) + " nickel\n" elif numberOfNickels > 1: s += "\t " + str(numberOfNickels) + " nickels\n" if numberOfPennies == 1: s += "\t " + str(numberOfPennies) + " penny" elif numberOfPennies > 1: s += "\t " + str(numberOfPennies) + " pennies" print(s)
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.