2017-09-24 95 views
0
def main(): 
    total = 0 
    print('Welcome to the Dew Drop Inn!') 
    print('Please enter your choices as y or n.') 
    print('  ') 
    cheese = (input('Do you want a grilled cheese? ')) 
    dog = (input('Do you want a hot dog? ')) 
    nacho = (input('Do you want nachos? ')) 
    burger = (input('Do you want a hamburger? ')) 
    grilled_cheese = 5 
    hot_dog = 5 
    nachos = 4 
    hamburger = 6 
    cheese_burger = 1 
    if cheese == 'y': 
     total + grilled_cheese 
    if dog == 'y': 
     total + hot_dog 
    if nacho == 'y': 
     total + nachos 
    if burger == 'y': 
     total + hamburger 
     the_cheese_please = input('Do you want cheese on that? ') 
     if the_cheese_please == 'y': 
      total + cheese_burger 
    else: 
     total + 0 

    print('  ') 
    print('The total for your food is $',(total),'.') 
    tip = total * 1.15 


main() 

我需要能夠從用戶告訴我加起來的數字。根據他們想要什麼食物,我該如何使用if/else語句來添加數字? if的最大數量是5,else的最大數量是1.我很新,所以我很抱歉如果這看起來很天真,但如果有人給我一個小費,這真的會幫助我。謝謝!使用if/else語句添加數字?

+1

到底是什麼錯,你發佈的代碼? – csmckelvey

+0

你是一個不錯的翻車手,確定它不應該是'tip = total * 0.15'? – Mitchel0022

+0

通過諸如pylint之類的工具或使用適當的python ide來運行此代碼可以很快地突出顯示此問題。 – Shadow

回答

2

total + grilled_cheese和其他類似的線路做沒什麼用處。他們將2個數字加在一起,但不要對結果做任何事情。這就像說一句,2只是浮在你的代碼中一樣。

您需要重新分配結果返回給total

total = total + grilled_cheese 

或者更簡潔

total += grilled_cheese 
1

在每個if語句你做:

total + grilled_cheese 

等,這是不是你遞增蟒蛇您的總價值的方式。

相反,你想做的事:

total += grilled_cheese 

並且不需要你else語句;如果你的if語句運行,那麼總數將增加,但如果它不運行(輸入是'n'),那麼總數不會改變。

你的代碼看起來幾乎一樣:

def main(): 
total = 0 
print('Welcome to the Dew Drop Inn!') 
print('Please enter your choices as y or n.') 
print('  ') 
cheese = (input('Do you want a grilled cheese? ')) 
dog = (input('Do you want a hot dog? ')) 
nacho = (input('Do you want nachos? ')) 
burger = (input('Do you want a hamburger? ')) 
grilled_cheese = 5 
hot_dog = 5 
nachos = 4 
hamburger = 6 
cheese_burger = 1 
if cheese == 'y': 
    total += grilled_cheese 
if dog == 'y': 
    total += hot_dog 
if nacho == 'y': 
    total += nachos 
if burger == 'y': 
    total += hamburger 
    the_cheese_please = input('Do you want cheese on that? ') 
    if the_cheese_please == 'y': 
     total += cheese_burger 


print('  ') 
print('The total for your food is $',(total),'.') 
tip = total * 1.15 


main()