私はプログラミングが初めてです。私は4つの基本的なプログラムを書いていますが、今は機能を学んでいます。私は機能を使用する船積みおよび取扱プログラムを作ろうとしていますが、私は立ち往生しています。説明は次のとおりです。配送、処理、税金を含む製品の総コストを計算するアプリケーションを作成します。これにはmain以外にも3つの機能が必要です。関数を使ったPythonの配送と計算機
最初の関数は、ユーザーに注文の小計を入力し、この情報をmainに返します。これらの値は検証する必要があります。小計は少なくとも$ 1で$ 9,999以下でなければなりません。
第2の関数は、輸送費と運賃を計算し、この情報をmainに返します。出荷は注文費用の10%です。たとえば、10ドルの注文の送料は1ドルです。 $ 100未満の注文の場合、処理は定額で$ 2です。それ以外の場合、処理は無料です。
第3の関数は、売上税を6%で計算し、この情報をmainに返します。売上税は小計にのみ基づいており、船積みや取り扱いには適用されません。
メインは、この情報を以下のサンプルのように画面に表示します。 (値を渡して返す必要があります)。
出力は次のようになります。
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()
はあなたのコードのインデントを修正してください、私たちは、あなたが試したもののことを確認することができます。 – cco
ok私はそれがすべて適切に今すぐラインを持っていると思います。 – hppylttletrees
'subtotal = calc_subtotal'は' calc_subtotal'を呼び出さないので、 'subtotal'を' calc_subtotal'関数への参照にします。 'subtotal = calc_subtotal()'は、 'calc_subtotal()'から返された結果を参照する '' subtotal''を呼び出します。その値をパラメータとする他の関数を定義することができます。 – cco