2016-05-26 13 views
0
# Determine price per pound 
if quantity >= 40: 
    print('Cost of coffee $', format(quantity * 7.50,'.2f'),sep='') 
else: 
    if quantity >= 20: 
     print ('Cost of coffee $', format(quantity * 8.75, '.2f'), sep='') 
    else: 
     if quantity >= 10: 
      print ('Cost of coffee $', format (quantity * 10.00, '.2f'), sep='') 
     else: 
      if quantity >= 1 or quantity <= 9: 
       print ('Cost of coffee $', format (quantity * 12.00, '.2f'), sep='') 

変数に割り当てられた合計(1ポンドあたりの金額*入力数量)がどのように得られるか把握しようとしています。私は税金の前に合計を取ることができ、7%の税金でそれを倍にすることができる必要があります。上記は数量と価格に基づいてどれくらいの費用がかかっているかを調べなければならない公式です。Pythonは変数名をif else関数に代入します。

+1

インデントを固定します。 –

+0

pythonに 'elif'ブロックがあります –

+0

' quantity 'が9より大きく10より小さい場合どうなりますか? –

答えて

1

したがって、総費用を把握するために別の変数を使用する必要があります。これは合計を使用するためです。次に、フローコントロールを使用してquantity * priceに設定することができます。このためには、さらにif, elif, elseを使用してください。

その後、総額を使って税金を計算することができます。これはかなり簡単です。

最後に、ifステートメントで使用したのと同じprintステートメントを使用して、合計コストを出力できます。

# initialize a variable to keep track of the total 
total = 0 

# Determine price per pound 
# use if elif 
if quantity >= 40: 
    total = quantity * 7.50 
elif quantity >= 20: 
    total = quantity * 8.75 
elif quantity >= 10: 
    total = quantity * 10.00 
else: 
    total = quantity * 12.00 


# do the tax calculations using total 
total = total * 1.07 

# print the result 
print('Cost of coffee $', format(total,'.2f'), sep='') 

税金を別々に計算して使用する場合は、別の変数を使用する必要があります。上記の例のように、合計を使用しました。今回は、taxという変数を追加します。

次に、別のprint文を追加して出力します。

# initialize a variable to keep track of the total 
total = 0 

# Determine price per pound 
# use if elif 
if quantity >= 40: 
    total = quantity * 7.50 
elif quantity >= 20: 
    total = quantity * 8.75 
elif quantity >= 10: 
    total = quantity * 10.00 
else: 
    total = quantity * 12.00 


# do the tax calculations assigning it to a different variable 
tax = total * 0.07 

# add the tax to the total 
total = total + tax 

# print the tax 
print('Tax $', format(tax,'.2f'), sep='') 

# print the total 
print('Cost of coffee $', format(total,'.2f'), sep='') 
+1

私はその最初の条件を '1 <=数量= 9'に変更します。また、他の条件も調整する必要があります。現在は、第2の条件を使用します(最初の条件を使用しない場合は100など)。 – L3viathan

+0

ありがとう、それはそこにかなりの脳のおならでした。 – bmcculley

+0

私はそこに別の脳の部分があると思うが、それはあなたがファターではないということだ:私は 'または'が 'と'でなければならないと思う。そうでなければ条件は 'else'で置き換えることができる。 1より大きいか9より小さいかを指定します。 – L3viathan

関連する問題