FOR PYTHON: Description: Design a program that will accept a price in cents, e.g
ID: 3804908 • Letter: F
Question
FOR PYTHON:
Description: Design a program that will accept a price in cents, e.g. 235 to represent a price such as$2.35. The amount must be a multiple of 5, since vending machines do not take pennies. (Your programmust check for valid input). The program should then figure out the number and type of coins that arerequired to purchase the item, using the guidelines given below. Write the program as a loop that stopswhen the user enters ‘done’ (without the quotes).
Input: The user should be prompted for a price in cents. When the user is finished, he/she will enter theword ‘done’ (no quotes).
Enter the price as cents, a multiple of five cents, or ‘done’ (without quotes) if you are finished.
Output: If the user entered ‘done’, say goodbye and stop. Otherwise, print what the user entered,formatted properly as dollars and cents, and the coins needed to buy it.
Example: Good-Bye! [If entered done]
Examples: For a $2.35 item, you need: 2 dollars, 1 quarter, 1 dime
Procedure: 1. Prompt the user for a price in total cents or done a. If the user enters done, say Good-Bye and stop. Otherwise assume that the input is a price in cents, e.g. for $2.35 the user would input 235. Your program does not have to check for non-number-format input. b. You will need to convert the input to an integer for the coin calculations: e.g.: price nt(inprice) c. If the price entered is not an integer multiple of 5, print an error message and prompt for input again 2. Determine which coins are needed: a. dollars, quarters, dimes, nickels no pennies) b. 1 dollar 100 cents, 1 quarter 25 cents 1 dime 10 cents, 1 nickel 5 cents 3. Print the coins needed. a. Output format should resemble For a $n.nn item, you need: n dollars n quarters n dimes n nickels b. If you only need 1 of a coin, you must print the singular noun: For a $2.35 item, you need: 2 dollars 1 quarter 1 dime c. Do not print a line for anything you need 0 of d. You must properly print the price in dollars and cents, including the cases where the price is a whole dollar or multiple of 10 cents. For example, you must print $3.50, not $3.5Explanation / Answer
#Hi,there is high posibility that while copying code identation problems
#may occur,please do take care,
#sample Input/Output
#Enter Number of Cents:3434
#re-enter,cents should be multiple of 5 only:
#Enter Number of Cents:345
#for a $ 3.45
#3 dollars
#1 quarters
#2 dimes
#ALL THE BEST
#!/usr/bin/python
from sys import exit;
cents=raw_input( "Enter Number of Cents:");
r = 1; #dummy remainder
while r != 0 : # This constructs an infinite loop to enter cents multiple of 5
if(cents=="done"):
print "Good-Bye";
exit(); #exiting if typed done;
cents=int(cents);
r = cents % 5;
if(r!=0):
print "re-enter,cents should be multiple of 5 only:"
cents=input("Enter Number of Cents:");#while ends
d=0;
q=0;
di=0;
ni=0;
r=1;
r=cents%100;
d=cents/100;
if(r!=0):#if starts
r=(cents-d*100)%25;
q=(cents-d*100)/25;#if ends
if(r!=0):#if starts
r=(cents-d*100-q*25)%10;
di=(cents-d*100-q*25)/10;#if ends
if(r!=0):#if starts
r=(cents-d*100-q*25-di*10)%5;
ni=(cents-d*100-q*25-di*10)/5#if ends
cents_to_dollars_format_string = '{:,.2f}';#formating for 2 decimal places
print "for a $",cents_to_dollars_format_string.format(cents/100.0);
if(d>0):
print d,"dollars";
if(q>0):
print q,"quarters";
if(di>0):
print di,"dimes";
if(ni>0):
print ni,"nickels";
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.