2017-09-24 10 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ステートメントを使用して番号を追加していますか?

total + grilled_cheese 

などこれは、あなたがpythonで合計のあなたの値をインクリメント方法ではありません。あなたが行っている、あなたのif文のそれぞれにおいて

+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

。代わりに

あなたがしたい:

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() 
関連する問題