python-3.x 我代码的最后一小部分有问题...数学相关?

sf6xfgos  于 2023-06-07  发布在  Python
关注(0)|答案(1)|浏览(252)

我正在写一个代码来计算一个三明治的价格和它的各部分的总和。但是,当输出配料的价格时,我经常得到0值或没有意义的高值。谁能看看我代码的最后14行,告诉我为什么我的数学不对?我已经在这上面工作了3个多小时了,我还是想不出来。谢谢

## 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}." )
31moq8wy

31moq8wy1#

是的,这可能不是一个数学问题,而是一个Python问题。JonSG在他的评论中是正确的:你不想把浮点数(比如5.25美元)转换成整数。因此,当您的用户输入任何东西(面包、奶酪或意大利香肠)的价格时,您需要确保将该数字解释为美元金额(这意味着,小数点后可能有两位的浮点数,而不是整数)。然后当你除法的时候,你要确保你做的是浮点数而不是整型数。好消息是,您已经正确地格式化了打印语句,将美元金额显示为具有两个小数位的浮点数。
你可以看看这些其他的问题,讨论如何判断某个东西是否是浮点数:
Check if a number is int or float
Checking if a string can be converted to float in Python

相关问题