0

I am writing a code to calculate the price of a sandwich and the sum of its parts. However, when the price of the ingredients is output, I often get 0 values or high values that make no sense. Can someone look at the last 14 lines of my code and tell me why my math isn't right? I've been working on this for 3+ hours and I can't figure it out. Thanks.


## Spencer Shample's Code

name_vendor = input('What is the name of the name of the vendor?\n')
    ## input the vendors name
while True:
    if name_vendor.isnumeric():
        name_vendor = input('Please enter the name of the vendor to continue.\n')
    else:
        break
    
    
price_loaf_bread = input('\nHow much does a loaf of bread cost?\n$')
    ## input loaf of bread price here
while True:
    if price_loaf_bread.isnumeric() and int(price_loaf_bread) > 0:
        break
    else:
       price_loaf_bread = input('Please enter the price of a loaf of bread to continue.\n$')
        

num_slices_bread_loaf = input('\nHow many slices in a loaf of bread?\n')
    ## input number of slices in a loaf of bread
while True:
    try:
        if int(num_slices_bread_loaf) < 1:
            raise ValueError #this will send it to the print message and back to the input option
        break
    except ValueError:
        num_slices_bread_loaf = input('You must have one or more slices of bread to make a sandwich!     \nPlease enter the number of slices of bread on your sandiwch(es)\n')    
    
    
price_cheese_per_lb = input('\nWhat is the price of swiss cheese per pound?\n$')
    ## price of cheese per lb
while True:
    if price_cheese_per_lb.isnumeric() and int(price_cheese_per_lb) > 0 :
        break
    else:
       price_cheese_per_lb = input('Please enter the price cheese per lb to continue.\n$')


num_slices_cheese = input('\nHow many slices in a pound of cheese?\n')
    ## number of slices of cheese in a lb
while True:
    try:
        if int(num_slices_cheese) < 1:
            raise ValueError #this will send it to the print message and back to the input option
        break
    except ValueError:
        num_slices_cheese = input('You must have one or more slices of cheese to make a sandwich!     \nPlease enter the number of slices of cheese on your sandiwch(es)\n')   


price_pastrami_per_lb = input('\nWhat is the price of pastrami per pound?\n$')
    ## price of pastrami per lb
while True:
    if price_pastrami_per_lb.isnumeric() and int(price_pastrami_per_lb) > 1:
        break
    else:
       price_pastrami_per_lb = input('Please enter the price pastrami per lb to continue.\n$')


num_slices_pastrami = input('\nHow many slices in a pound of pastrami?\n')
    ## number of slices of pastrami in a lb
while True:
    try:
        if int(num_slices_pastrami) < 1:
            raise ValueError #this will send it to the print message and back to the input option
        break
    except ValueError:
        num_slices_pastrami = input('You must have one or more slices of pastrami to make a sandwich!     
        \nPlease enter the number of slices of pastrami on your sandiwch(es)\n')       
    
dollar_per_slice_bread = (int(price_loaf_bread) / int(num_slices_bread_loaf)) 
input_num_slices_bread_loaf = input('How many slices of bread are you using on your sandwich?\n')
total_price_bread = (int(dollar_per_slice_bread) * int(input_num_slices_bread_loaf))


dollar_per_slice_cheese = (int(price_cheese_per_lb) / int(num_slices_cheese))
input_num_slices_cheese = input('How many slices of cheese are you using on your sandwich?\n')
total_price_cheese = (int(dollar_per_slice_cheese) * int(input_num_slices_cheese))


dollar_per_slice_pastrami = (int(price_pastrami_per_lb) / int(num_slices_pastrami)) 
input_num_slices_pastrami = input('How many slices of pastrami are you using on your sandwich?\n')
total_price_pastrami = (int(dollar_per_slice_pastrami) * int(num_slices_pastrami))


print(f"\nThe cost of the bread is ${total_price_bread:.2f}\n")
print(f"\nThe cost of the cheese is ${total_price_cheese:.2f}\n")
print(f"\nThe cost of the pastrami is ${total_price_pastrami:.2f}\n")

total_cost_sandwich = (total_price_bread + total_price_cheese + total_price_pastrami)
num_sandiwches = input('How many sandwiches are you making?\n')
print(f"If you are making {num_sandiwches} sandiwches then the total cost per sandwich from {name_vendor} is ${total_cost_sandwich:.2f}." )   
Spencer
  • 3
  • 1
  • once you calculate `dollar_per_slice_bread` as a number (likely a float) you don't want to then cast it to an int to use it. – JonSG Jun 02 '23 at 16:30

1 Answers1

0

Yes, this is likely not a math problem, but a python problem. JonSG was right in his comment: you don't want to cast floating point numbers (like $5.25) as an integer. So when your user inputs a price of anything (bread or cheese or salami), you want to make sure to interpret that number as a dollar amount (meaning, a float with probably two places after the decimal and not an integer). And then when you divide, you want to make sure that you are doing the arithmetic as floats not ints. The good news is that you have formatted your print statements correctly to show dollar amounts as floating points numbers with two decimal places.

You might have a look at these other questions that talk about how to tell if something is a float:

Check if a number is int or float

Checking if a string can be converted to float in Python

Ann Dorian
  • 41
  • 3