2

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:

  1. Ask the user for the price of a child and an adult meal and store these values properly into variables as floating point numbers.

  2. Ask the user for the number of adults and children and store these values properly into variables as integers.

  3. Ask the user for the sales tax rate and store the value properly as a floating point number.

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

Erin McKee
  • 35
  • 5
  • 1
    Floating-point values are, by necessity, imprecise. The easiest solution is to use the round function, i.e. `total = round(total, 2)` to round to two digits of precision (pennies). – John Gordon Jan 11 '23 at 19:35
  • 2
    It does not look like you are doing anything wrong rather you are experiencing a quirk of floating point numbers in general (not python specific). Check out https://stackoverflow.com/questions/588004/is-floating-point-math-broken it is likely your answer – JonSG Jan 11 '23 at 19:37
  • Also, the `input` function returns a string which means your `childsmeal`, etc.. variables contain strings. Passing a string into the `float` and `int` functions will return a `float` and `int`. So in effect, `costchild`, `costadult`, etc. contain `float`s. If you one to follow 1. to the letter, you could just pass `input("What is the price of the child's meal? ")` directly to `float` and assign the result to `childsmeal`. Example: `childsmeal = float(input("What is the price of the child's meal? "))` – Axe319 Jan 11 '23 at 19:40
  • @JohnGordon The instructor asked specifically that they be stored as floating point numbers and integers. Did I already do that and not even know it? – Erin McKee Jan 11 '23 at 19:42
  • @Axe319 how would I store them as an integer instead of a float? Should I say int(childsmeal) instead of float(childsmeal)? I'm just confused with the terminology of computer programming. Like what everything is called. – Erin McKee Jan 11 '23 at 19:43
  • 2
    Something like `numchild = int(input("How many children? "))` would work. Also, if you assign them directly as `int`s and `float`s from `input()`, you won't need to wrap your variables in your calculations with `float`. – Axe319 Jan 11 '23 at 19:44
  • Thank you! I will try that for the integer part of my problem! – Erin McKee Jan 11 '23 at 19:47
  • 1
    A little tip, if you're confused about what the type of a variable is, you can always `print(type(my_variable))` while debugging. – Axe319 Jan 11 '23 at 19:48
  • @Axe319 where in the code would I put that function? And would it work for all my variables or just the one it is closest to? You are awesome BTW Thank you! – Erin McKee Jan 11 '23 at 19:50
  • 1
    At any point after it has something assigned to it. And it works for any variable. For example: `childsmeal = input("What is the price of the child's meal? ")` `print(type(childsmeal))`. For the output portion of the assignment (adding the `$` and decimal places), look into string formatting. There should be plenty of beginner friendly resources. – Axe319 Jan 11 '23 at 19:56
  • @JohnGordon "Floating-point values are, by necessity, imprecise. " --> no. FP values are very precise. Each finite value is exact. It is the FP operations that can differ slightly from math are the source of typical issues. – chux - Reinstate Monica Jan 11 '23 at 22:07
  • @ErinMcKee "What am I doing wrong?" --> code needs to _round_ to get expected results. Consider scaling to _cents_ too. – chux - Reinstate Monica Jan 11 '23 at 22:13

1 Answers1

0

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.

bph
  • 59
  • 4
  • So I did it right? I just need to round it? And is there a way to have the program know that it's in monetary form? The assignment says it has to have the $$$ before the number in the output. – Erin McKee Jan 11 '23 at 19:46
  • "For eons and eons, computers have had trouble with decimal numbers" --> many early computers used decimal floating point and had far fewer issue, so not quite eons. – chux - Reinstate Monica Jan 11 '23 at 22:11
  • @chux-ReinstateMonica - Exaggeration for humour? – bph Jan 12 '23 at 02:14
  • @ErinMcKee - I don't believe that Python has a separate type for "money" (some platforms do). I would suggest that you round the answer and append the currency symbol. – bph Jan 12 '23 at 02:15