My homework is to write a program that asks the user to input the price of an adult's and a child's meal, then asks how many of each there are, then asks the sales tax rate, then asks how much you are giving in money. It should then output the subtotal then the total with sales tax, then it should subtract how much the total is from how much money the user is "paying with" (Ex: total is $12.50 and they give you $20, it's supposed to give you the change amount)
My trouble is with knowing how to store a variable the user inputs as a floating point number and an integer. I don't know what the difference is and how to put it in the code. Technically my program runs and works but it's not how the instructions ask you to do it. I also get way to many decimal places when it gives you the change. How do I have the program know that it needs to be in monetary form? This is the instructions that the instructor gives for the first part:
Ask the user for the price of a child and an adult meal and store these values properly into variables as floating point numbers.
Ask the user for the number of adults and children and store these values properly into variables as integers.
Ask the user for the sales tax rate and store the value properly as a floating point number.
Compute and display the subtotal (don't worry about rounding to two decimals at this point).
print("Please fill out the following: ")
childsmeal = input ("What is the price of the child's meal? ")
adultmeal = input ("What is the price of the adult's meal? ")
numchild = input("How many children? ")
numadult = input("How many adults? ")
tax = input("What is the sales tax rate? ")
payment = input("Please enter your payment amount: ")
costchild = float(childsmeal) * float(numchild)
costadult = float(adultmeal) * float(numadult)
mealcost = float(costchild) float(costadult)
mealtax = float(mealcost) * float(tax) / 100
total = float(mealtax) float(mealcost)
change = float(payment) - float(total)
print("Subtotal:", mealcost)
print("Sales Tax:", mealtax)
print("Total:", total)
print("Change:", change)
An example output would be:
**Please fill out the following:
What is the price of the child's meal? 4.50
What is the price of the adult's meal? 9.00
How many children? 3
How many adults? 2
What is the sales tax rate? 6
Please enter your payment amount: 40
Subtotal: 31.5
Sales Tax: 1.89
Total: 33.39
Change: 6.609999999999999**
What am I doing wrong? This is a very basic version of my finished program. I'm just trying to get down the basics of it before I add more flourish to make it look nice.
CodePudding user response:
What are you doing wrong? - you can fix your result by rounding the final answer to two decimal places.
For eons and eons, computers have had trouble with decimal numbers, and there's always a risk of "imprecision" unless they are stored and handled (internally) in ways to avoid it.... Look up "Binary Coded Decimal" or BCD if you want to explore the background.
You should also become familiar with data types. Here are some references for data types in Python:
Integers are "whole numbers" 1, 2 and 3 for example (and they include negative numbers). Decimal numbers are numbers that can be fractional -- that have values after the decimal point. In computers, they are often called "floating point" numbers because the programmer can decide how much precision to store. In mathematics, the whole number "0" does not always have the same meaning as "0.000000000000" if you are concerned about precision or significant digits.
CodePudding user response:
Here is what you are doing wrong:
replace:
costchild = float(childsmeal) * float(numchild)
costadult = float(adultmeal) * float(numadult)
with:
numchildint = int(numchild)
costadultint = intt(numadult)
costchild = float(childsmeal) * numchildint
costadult = float(adultmeal) * numadultint
--per "Ask the user for the number of adults and children and store these values properly into variables as integers"
The instructions also mention storing the floats. You're displaying more than asked for. The instructions say to don't worry about rounding (BTW, best sol'n is to do all the math in cents as ints--avoid floats).