为什么我的代码在python中给予我错误的结果,甚至相同的代码也给我不同的结果?

jk9hmnmh  于 2023-02-20  发布在  Python
关注(0)|答案(2)|浏览(127)
bill = 0
if size == "S":
   bill += 10
if size == "M":
   bill += 15 
if size == "L":
   bill += 20
if Pepperoni == "Yes" and size == "S":
  if Pepperoni == "Yes" and size == "M" or size == "L":
     bill += 2   
if extra_cheese == "Yes":
     bill += 1  
print(f"Your final bill is ${bill}")

欢迎光临python比萨外卖!您想要多大的比萨?S,M还是L?M您想要意大利辣香肠吗?是或否?是的您想要额外的奶酪吗?是或否?是的您的最终账单是16美元
你的短信?

bfrts1fy

bfrts1fy1#

if Pepperoni == "Yes" and size == "S":
  if Pepperoni == "Yes" and size == "M" or size == "L":
     bill += 2

第二个if条件永远不可能为真,因为它只在我们已经知道size为“S”时才执行。

ddrv8njm

ddrv8njm2#

bill = 0
if size == "S":
    bill += 10
if size == "M":
    bill += 15 
if size == "L":
    bill += 20
if Pepperoni == "Yes" and size == "S":
    """"Do somthing?????"""
if Pepperoni == "Yes" and size == "M" or size == "L":
    bill += 2   
if extra_cheese == "Yes":
    bill += 1  
print(f"Your final bill is ${bill}")

相关问题