SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.selections
Exercise:
Programming Excercises
Question:7 | ISBN:978013274719 | Edition: 6

Question

(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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

print("Enter an amount in double: ")
amount = float(input())
remainingAmount = int(amount * 100)
#Find the number of one dollars
numberOfOneDollars = remainingAmount / 100
remainingAmount = remainingAmount % 100
#Find the number of quarters in the remaining amount
numberOfQuarters = remainingAmount / 25
remainingAmount = remainingAmount % 25
#Find the number of dimes in the remaining amount
numberOfDimes = remainingAmount / 10
remainingAmount = remainingAmount % 10
#Find the number of nickels in the remaining amount
numberOfNickels = remainingAmount / 5
remainingAmount = remainingAmount % 5
#Find the number of pennies in the remaining amount
numberOfPennies = remainingAmount
#Display results
print("Your amount {} consists of".format(amount))
if (numberOfOneDollars == 1):
    print("   {} dollar     ".format(numberOfOneDollars))
elif (numberOfOneDollars > 1):
    print("    {} dollars".format(numberOfOneDollars))
if (numberOfQuarters == 1):
    print("     {} quarter ".format(numberOfQuarters))
elif (numberOfQuarters > 1):
    print("     {} quarters ".format(numberOfQuarters))
if (numberOfDimes == 1):
    print("     {} dime ".format(numberOfDimes))
elif (numberOfDimes > 1):
    print("    {} dimes ".format(numberOfDimes))
if (numberOfDimes == 1):
    print("     {} nickel ".format(numberOfNickels))
elif (numberOfDimes > 1): 
    print("     {} nickels".format(numberOfNickels))
if (numberOfPennies == 1):
    print("     {} penny".format(numberOfPennies))
elif (numberOfPennies > 1):
    print("    {} pennies".format(numberOfPennies))

Output:

Enter an amount in double: 
11.5
Your amount 11.5 consists of
    11.5 dollars
     2.0 quarters 

0 0

Discussions

Post the discussion to improve the above solution.