debugging 使用IF ELIF和ELSE语句进行Python调试

kyvafyod  于 2022-11-14  发布在  Python
关注(0)|答案(2)|浏览(145)

我需要编写代码,正确地输出所列食品组合的正确成本。我知道正确的金额是6.50美元,但我一直得到6.00美元。

item = "nachos"
meat = "steak"
queso = True
guacamole = False
double_meat = True

#-----------------------------------------------------------
#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.
#
#Let's further expand our previous program to cover a broader
#menu variety. Instead of just burritos, now the program
#should cover three menu items: quesadillas, burritos, and
#nachos. Instead of separate booleans for steak and pork,
#we instead have a string that could be "steak", "pork",
#"chicken", "tofu", and "beef". We still have booleans for
#queso and guacamole, but we also have a boolean for double
#meat.
#
#Your code should calculate the price as follows:
#
# - The base price for a quesadilla is 4.00, for nachos is
#   4.50, and for burritos is 5.00.
# - If meat is steak or pork, add 0.50. Any other meat adds
#   no money to the price.
# - guacamole always adds 1.00 to the price.
# - queso adds 1.00 to the price UNLESS the item is nachos,
#   in which case it adds nothing.
# - double_meat adds 1.50 if the meat is steak or pork, or
#   1.00 otherwise.

if item == "quesadilla":
    base_price = 4.0
    if meat == "steak" or meat == "pork":
        base_price += 0.50
    if guacamole:
        base_price += 1.00
    if queso:
        base_price += 1.00
    if double_meat == "steak" or double_meat == "pork":
        base_price += 1.50
    else:
        base_price += 1.00
       
elif item == "nachos":
    base_price = 4.50
    if meat == "steak" or meat == "pork":
        base_price += 0.50   
    if guacamole:
        base_price += 1.00
    if double_meat == "steak" or double_meat == "pork":
        base_price += 1.50
    else:
        base_price += 1.00   

elif item == "burrito":
    base_price = 5.0
    if meat == "steak" or meat == "pork":
        base_price += 0.50
    if guacamole:
        base_price += 1.00
    if queso:
        base_price += 1.00
    if double_meat == "steak" or double_meat == "pork":
        base_price += 1.50
    else:
        base_price += 1.00

print(base_price)

根据我收集的信息,我的代码跳过了

"if double_meat == "steak" or double_meat == "pork":
   
 base_price += 1.50"

部分代码,直接进入

"else:
    base_price += 1.00"

部分,我似乎想不出为什么。任何帮助都将不胜感激。

bfnvny8b

bfnvny8b1#

检查与double_meat进行比较的数据类型,在本例中它不是布尔值,因为您在上面将其设置为up。

zwghvu4y

zwghvu4y2#

您在这里将值与字符串进行比较;但是你在上面声明了布尔值。现在你声明了“True”,它就可以工作了

item = "nachos"
meat = "steak"
queso = True
guacamole = False
double_meat = True

if item == "quesadilla":
   base_price = 4.0
   if meat == "steak" or meat == "pork":
       base_price += 0.50
   if guacamole:
       base_price += 1.00
   if queso:
       base_price += 1.00
   if double_meat or double_meat:
       base_price += 1.50
   else:
       base_price += 1.00
      
elif item == "nachos":
   base_price = 4.50
   if meat == "steak" or meat == "pork":
       base_price += 0.50
       
   if guacamole:
       base_price += 1.00
     
   if double_meat or double_meat: #it now validating true/false
       base_price += 1.50
   else:
       base_price += 1.00

elif item == "burrito":
   base_price = 5.0
   if meat == "steak" or meat == "pork":
       base_price += 0.50
   if guacamole:
       base_price += 1.00
   if queso:
       base_price += 1.00
   if double_meat or double_meat:
       base_price += 1.50
   else:
       base_price += 1.00

print(base_price)

相关问题