2017-02-22 17 views
0

私はプログラミングが初めてです。私は4つの基本的なプログラムを書いていますが、今は機能を学んでいます。私は機能を使用する船積みおよび取扱プログラムを作ろうとしていますが、私は立ち往生しています。説明は次のとおりです。配送、処理、税金を含む製品の総コストを計算するアプリケーションを作成します。これにはmain以外にも3つの機能が必要です。関数を使ったPythonの配送と計算機

  1. 最初の関数は、ユーザーに注文の小計を入力し、この情報をmainに返します。これらの値は検証する必要があります。小計は少なくとも$ 1で$ 9,999以下でなければなりません。

  2. 第2の関数は、輸送費と運賃を計算し、この情報をmainに返します。出荷は注文費用の10%です。たとえば、10ドルの注文の送料は1ドルです。 $ 100未満の注文の場合、処理は定額で$ 2です。それ以外の場合、処理は無料です。

  3. 第3の関数は、売上税を6%で計算し、この情報をmainに返します。売上税は小計にのみ基づいており、船積みや取り扱いには適用されません。

  4. メインは、この情報を以下のサンプルのように画面に表示します。 (値を渡して返す必要があります)。

出力は次のようになります。

Product Total Information 
     Subtotal       $300.00 
     Shipping       $30.00 
     Handling        $0.00 
     Sales tax       $18.00 
     Grand total      $348.00 

は、ここで私がこれまで持っているものだ: 私はこれまで全く新しいです覚えておいてください。ここに私のメインです:小計検証のために

def main(): 
    subtotal = calc_subtotal 
    tax = calc_tax 
    shipping = calc_shipping 
    handling = calc_handling 
    print('subtotal: ') 
    print('Shipping: ') 
    print('Handling: ') 
    print('Sales tax: ') 
    print('Grand total: ') 

私は他/ if文を書いた:

def calc_subtotal(): 
    subtotal = float(input('Enter your subtotal please: ')) 
    if subtotal >= 1 and subtotal <= 9999: 
     print('This is a valid amount') 
    else: 
     print('Invalid amount') 
    return subtotal 

今、私はちょうど税額を把握するために、単純な何かをしなければならない把握、など次のように: 小計* .06

道順が書かれている通り、同じ機能で配送と処理の計算ができるようですか?

私が苦労している部分は、小計の値を他の関数に移動する方法です。税務機能の場合と同様に、私が必要とするのは.06でそれを掛けることだけですが、その関数に呼び出す方法を理解することはできません。私はこれが長いことを知っています。おそらくあなたは多分私が答えることができないだろうが、あなたが私がそれを感謝するのを助けたいと思っている場合は、たくさんの質問があります。読んでくれてありがとう。

ここでは、更新プログラムのコードは、2017年2月23日のようだ:

def main(): 
    print('This will calculate shipping, handling and taxes on your purchase') 
    subtotal = calc_subtotal() 
    tax = calc_tax() 
    shipping = calc_shipping() 
    handling = calc_handling() 
    total = calc_total() 
    print('subtotal: ', subtotal) 
    print('Shipping: ', shipping) 
    print('Handling: ', handling) 
    print('Sales tax: ', tax) 
    print('Grand total: ', total) 

def calc_subtotal(): 
    subtotal = float(input('Enter your subtotal please: ')) 
    if subtotal >= 1 and subtotal <= 9999: 
     print('This is a valid amount') 

    else: 
     print('Invalid amount') 
     return subtotal 


def calc_tax(): 
    subtotal = calc_subtotal() 
    tax = subtotal + subtotal * .06 
    return tax 

def calc_handling(): 
    subtotal = calc_subtotal() 
    if subtotal < 100: 
     print('There is a 2 dollar handling fee') 
    else: 
     print('There is no handling fee') 

def calc_shipping(): 
    subtotal = calc_subtotal() 
    shipping = subtotal + subtotal * .10 
    print('Your shipping cost is: ', shipping) 

def calc_total(): 
    total = subtotal + shipping + handling + tax 



main() 
+0

はあなたのコードのインデントを修正してください、私たちは、あなたが試したもののことを確認することができます。 – cco

+0

ok私はそれがすべて適切に今すぐラインを持っていると思います。 – hppylttletrees

+0

'subtotal = calc_subtotal'は' calc_subtotal'を呼び出さないので、 'subtotal'を' calc_subtotal'関数への参照にします。 'subtotal = calc_subtotal()'は、 'calc_subtotal()'から返された結果を参照する '' subtotal''を呼び出します。その値をパラメータとする他の関数を定義することができます。 – cco

答えて

0

私は苦労してる部分が他の機能に行く小計値を取得する方法ですか?ユーザーは続行するために有効な入力を提供しなければならないように、私はしばらくループを追加しました次のコードで

は、の値を返すのではなく、subtotalと他の累積された値(各関数で計算された)を渡して、print文が繰り返されないように呼び出される先行する関数に渡しました。

def main(): 
    print('This will calculate shipping, handling and taxes on your purchase') 
    calc_subtotal() 

def calc_subtotal(): 
    while True: 
     subtotal = float(input('Enter your subtotal please: ')) 
     if subtotal >= 1 and subtotal <= 9999: 
      print('This is a valid amount') 
      break 
     else: 
      print('Invalid amount') 
      continue 
    calc_shipping(subtotal) 

def calc_shipping(subtotal): 
    shipping_cost = subtotal * .10 
    calc_handling(subtotal, shipping_cost) 

def calc_handling(subtotal, shipping_cost): 
    handling_fee = 0 
    if subtotal < 100: 
     print('There is a 2 dollar handling fee') 
     handling_fee += 2 
    else: 
     print('There is no handling fee') 
    calc_tax(subtotal, shipping_cost, handling_fee) 

def calc_tax(subtotal, shipping_cost, handling_fee): 
    tax = subtotal * .06 
    calc_total(subtotal, shipping_cost, handling_fee, tax) 

def calc_total(subtotal, shipping_cost, handling_fee, tax): 
    print('\nProduct Total Information') 
    print('\tSubtotal: ${}'.format(subtotal)) 
    print('\tShipping: ${}'.format(shipping_cost)) 
    print('\tHandling: ${}'.format(handling_fee)) 
    print('\tSales tax: ${}'.format(tax)) 
    total = subtotal + shipping_cost + handling_fee + tax 
    print('\tGrand total: ${}'.format(total)) 

main() 

出力:

This will calculate shipping, handling and taxes on your purchase 
Enter your subtotal please: 300 
This is a valid amount 
There is no handling fee 

Product Total Information 
    Subtotal: $300.0 
    Shipping: $30.0 
    Handling: $0 
    Sales tax: $18.0 
    Grand total: $348.0 
+0

なぜ私の小計機能は入力をもう求めていないのですか?私はそれをテストするために実行すると、私はちょうどメインから表示するために、何も印刷機能を取得します。何かアドバイス?私は彼が少しコードを整理しました、あなたはちょうどこのサイトの元のポストの下にそれを追加しますか? – hppylttletrees

+0

質問にコードを追加するには、編集オプションをクリックするだけです。 – smoggers

+0

よろしくお願い致します。私は今までの残りの部分を追加しました。あなたが見て、私にそれを感謝したいいくつかのフィードバックを与えたい場合。再度ありがとう – hppylttletrees

関連する問題