2016-05-07 6 views
-2
while True: 
    user_wants = raw_input("What price should be added to meal? (tax, tip, both) ") 
    if user_wants == 'tax': 
      cost_tax = input("What is the meal's cost? ") 
      tax_cost = 1.08 * cost_tax 
      tax = tax_cost - cost_tax 
      print "The price with tax is %s, tax is %s" % (tax_cost, tax) 
      continue_tax = raw_input("Do you want to continue? (y/n) ") 
      if continue_tax == 'y': 
     continue 
    elif continue_tax == 'n': 
     break 
    else: 
     #What to put here? 
    elif user_wants == 'tip': 
      cost_tip = input("What is the meal's cost? ") 
      tip_cost = 1.10 * cost_tip 
      tip = tip_cost - cost_tip 
      print "The price with tip is %s, tip is %s" % (tip_cost, tip) 
      break 
    elif user_wants == 'both': 
      cost_both = input("What is the meal's cost? ") 
      both_cost = (1.08 * cost_both - cost_both) + (1.10 * cost_both - cost_both) + cost_both 
      both = both_cost - cost_both 
      print ("The price with both tip and tax is %s, tax + tip is %s" % (both_cost, both)) 
      break 
    else: 
    print ("I didn't catched that please enter again ") 

私はオンラインで検索しましたが、役立つものが見つかりませんでした。Pythonで入力を再利用するには?

+1

を試してみてください。あなたの最終的な 'print'はインデントされるべきです。私はちょうど内側の 'if'を見つけましたが、あなたの圧痕はどこにでもあります。あなたはPythonで一貫した字下げを使用する必要があります - これはオプションではありません。 – cdarke

答えて

0

あなたの心に留めていることは明らかではありませんが、私は3つの場所で食事代を尋ねることに気付きます。このような流れはどう思いますか?

meal_price = raw_input("Price of meal: ") 
tax_rate = raw_input("Percent tax to add: ") 
tip_rate = raw_input("Percent tip to add: ") 
tax_amount = meal_price * tax_rate 
tip_amount = meal_price * tip_rate 
print("Tip amount: %5.2f" % tip_amount) 
print("Tax amount: %5.2f" % tax_amount) 
total = meal_price + tax_amount + tip_amount 
print("Meal total: %5.2f" % total) 

これはフローを簡略化し、ユーザーがチップや税金を追加したくない場合、そのプロンプトに0を入れるだけです。しかし、入力検証(ユーザーが適切な範囲で数値を入力したことを確認する)とエラー処理が必要です。 `、それは` elif`s後に来なければならないが、それはオプションです:

0

あなたは一つだけ `他に持つことができ、この

while True: 
    user_wants = raw_input("What price should be added to meal? (tax, tip, both) ") 
    if user_wants == 'tax': 
     cost_tax = input("What is the meal's cost? ") 
     tax_cost = 1.08 * cost_tax 
     tax = tax_cost - cost_tax 
     print "The price with tax is %s, tax is %s" % (tax_cost, tax) 
     continue_tax = raw_input("Do you want to continue? (y/n) ") 
     if continue_tax == 'y': 
      continue 
     break 
    elif user_wants == 'tip': 
     cost_tip = input("What is the meal's cost? ") 
     tip_cost = 1.10 * cost_tip 
     tip = tip_cost - cost_tip 
     print "The price with tip is %s, tip is %s" % (tip_cost, tip) 
     break 
    elif user_wants == 'both': 
     cost_both = input("What is the meal's cost? ") 
     both_cost = (1.08 * cost_both - cost_both) + (1.10 * cost_both - cost_both) + cost_both 
     both = both_cost - cost_both 
     print ("The price with both tip and tax is %s, tax + tip is %s" % (both_cost, both)) 
     break 
    else: 
     print ("I didn't catched that please enter again ") 
関連する問題