[英]Python Debugging Using IF ELIF and ELSE Statements
所以我目前正在解決這個調試問題,並且已經卡了幾個小時,所以我想我會試一試。 這是問題所在,我需要編寫代碼,以正確 output 列出所列食品的組合的適當成本。 我知道正確的金額是 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"
部分,我似乎無法弄清楚為什么。 任何幫助,將不勝感激。
檢查您正在比較double_meat
的數據類型,在這種情況下它不是 boolean,因為您將其設置為上面。
您在這里將值與字符串進行比較; 但是您在上面聲明了它 boolean 。 現在,當您宣布“真實”時,它將起作用
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.