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:
Tony Gaddis
Chapter:
Decision Structures And Boolean Logic
Exercise:
Programming Exercises
Question:6 | ISBN:9780132576376 | Edition: 2

Question

Change for a Dollar Game
Create a change-counting game that gets the user to enter the number of coins required to make exactly one dollar. The program should prompt the user to enter the number of pennies, nickels, dimes, and quarters. If the total value of the coins entered is equal to one dollar, the program should congratulate the user for winning the game. Otherwise, the program should display a message indicating whether the amount entered was more than or less than one dollar.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Change for a Dollar Game

CODE:

Pennies = int(input("Enter number of pennies :"))       #enter input number of pennies.
Nickels = int(input("Enter number of Nickels :"))       #enter input number of nickels.
Dimes = int(input("Enter number of Dimes :"))           #enter input number of dimes.
Quarters = int(input("Enter number of Quarters :"))     #enter input number of quarters.
Penny = 0.01                 #assiging penny,nickel,dime,quarter values.
Nickel = 0.05
Dime = 0.10
Quarter = 0.25
Pennies *= Penny          #pennies = pennies * penny (multiplying input value with assigned values)
Nickels *= Nickel
Dimes *= Dime
Quarters *= Quarter
Total = int(Pennies + Nickel + Dimes + Quarters)   #adding all values
print(Total)                                  #display total.
if Total == 1.00 :                                #check whether total is equal to 1 .
    print("Congratulations !\n'You won the game' The amount you entered equals 1 dollar.") #if yes diplay this message.
else:                                                #else total is not equal to 1.
    print("Better luck next time!\n 'you lost.'")       #display this message. 
    if Total >1.00 :                                  # and also check whether the unequaled total is greater than or less than 1. 
        print("The amount you entered is greater than 1 dollar.")    #display corresponding message.
    else:

 

 

OUPUT:

Enter number of pennies :1
Enter number of Nickels :2
Enter number of Dimes :3
Enter number of Quarters :4
1
Congratulations !
'You won the game' The amount you entered equals 1 dollar.



Enter number of pennies :12
Enter number of Nickels :54
Enter number of Dimes :65
Enter number of Quarters :8
8
Better luck next time!
 'you lost.'
The amount you entered is greater than 1 dollar.

 

0 0

Discussions

Post the discussion to improve the above solution.