2016-09-23 16 views
-1
#Initialization 
count=0 
name=0 

#Input 
while name!='-999': 
    count=count+1 
    name=input("Enter stock name OR -999 to Quit:") 
    shares=int(input("Enter number of shares:")) 
    pp=float(input("Enter purchase price:")) 
    sp=float(input("Enter selling price:")) 
    commission=float(input("Enter commission:")) 


#Calculations 
amount_paid=shares*pp 
commission_paid_purchase=amount_paid*commission 
amount_sold=shares*sp 
commission_paid_sale=amount_sold*commission 
profit_loss=(amount_sold - commission_paid_sale) -(amount_paid + commission_paid_purchase) 

#Output 
print("Stock Name:", name) 
print("Amount paid for the stock:  $", format(amount_paid, '10,.2f')) 
print("Commission paid on the purchase: $", format(commission_paid_purchase, '10,.2f')) 
print("Amount the stock sold for:  $", format(amount_sold, '10,.2f')) 
print("Commission paid on the sale:  $", format(commission_paid_sale, '10,.2f')) 
print("Profit (or loss if negative): $", format(profit_loss, '10,.2f')) 

===============================OUTPUT=================================================== 
Enter stock name OR -999 to Quit:Google 
Enter number of shares:10000 
Enter purchase price:39.99 
Enter selling price:35.99 
Enter commission:0.04 
Enter stock name OR -999 to Quit:Amazon 
Enter number of shares:10000 
Enter purchase price:15.99 
Enter selling price:33.99 
Enter commission:0.04 
Enter stock name OR -999 to Quit:-999 
Enter number of shares:10000 
Enter purchase price:14.99 
Enter selling price:49.99 
Enter commission:0.04 
Stock Name: -999 
Amount paid for the stock:  $ 149,900.00 
Commission paid on the purchase: $ 5,996.00 
Amount the stock sold for:  $ 499,900.00 
Commission paid on the sale:  $ 19,996.00 
Profit (or loss if negative): $ 324,008.00 

GoogleとAmazonの在庫を入力していますが、「-999」を実行すると印刷されませんwhileループから脱出する。代わりに、 "-999"はストック名であり、偽の番号を入力して印刷すると終了すると考えています。私は何が間違っているのか分かりません。ループが入力を印刷しないうちに、ループが中断しない

+0

赤いニシンかもしれませんが、 'while name!=' - 999 ':' not 'while while name!= " - 999" '? – XtrmJosh

+1

@XtrmJoshそれはまったく同じです... – Bharel

+0

私は@Bharelを期待していますが、一番矛盾しています:) – XtrmJosh

答えて

0

最後まで現在の反復を続ける代わりに、ループが-999になるとすぐに中断してください。

#Initialization 
count=0 
name=0 
result = '' 
#Input 
while True: 
    count=count+1 
    test=input("Enter stock name OR -999 to Quit:") 
    if test=='-999': break 
    name=test 
    shares=int(input("Enter number of shares:")) 
    pp=float(input("Enter purchase price:")) 
    sp=float(input("Enter selling price:")) 
    commission=float(input("Enter commission:")) 

    #Calculations 
    amount_paid=shares*pp 
    commission_paid_purchase=amount_paid*commission 
    amount_sold=shares*sp 
    commission_paid_sale=amount_sold*commission 
    profit_loss=(amount_sold - commission_paid_sale) -(amount_paid + commission_paid_purchase) 

    #Output 
    result += """ 
Stock Name: {} 
Amount paid for the stock:  ${:06.2f} 
Commission paid on the purchase: ${:06.2f} 
Amount the stock sold for:  ${:06.2f} 
Commission paid on the sale:  ${:06.2f} 
Profit (or loss if negative): ${:06.2f} 
""".format(name, amount_paid, commission_paid_purchase, amount_sold, commission_paid_sale, profit_loss) 

print (result); 

あなたが-999を入力する前に、それはあなたがnameの値をチェックしているrepl.it

+0

私はこれをやったことがありますが、おかげさまで株 "-999"の名前が付けられましたが、計算は正しく行われます。 –

+0

私はコードを編集して別の一時変数を使用しました。これは-999でないときにのみ名前に割り当てられます。 – trincot

+0

これで最後に入力した株の正しい名前が印刷されるようになりましたが、すべての株が表示されるわけではありません。 3つの株を入力して3つの出力を得るために、コードを修正するにはどうすればよいですか? whileループの中ですべての計算を動かすと、前と同じ結果が出ました... –

0

上で実行を参照してください:

第二に、あなたはループの後という文字列と出力だけでアイテムを収集でき。これにあなたのループを変更しますと

while True: 
    x = input("Enter stock name OR -999 to Quit:") 
    if x == '-999': 
     break 
    count = count+1 
    name = x 
    shares = int(input("Enter number of shares:")) 
    pp = float(input("Enter purchase price:")) 
    sp = float(input("Enter selling price:")) 
    commission = float(input("Enter commission:")) 

は、スクリプトが唯一あなたが入力した 最後株式の情報を処理しようとしています。ループの各繰り返しは、前の値 name, shares, pp, spおよび commissionを上書きします。ループ内で計算を移動するか、 すべてのデータをリストまたは辞書に保存して、後でそのデータにアクセスできるようにします。

+0

私はこれをしました、ありがとう、しかしそれは株 "-999"の名前をつけますが、計算は正しく行います。 –

+0

異なる変数を使用して株式名を入力し、 'if'ステートメントの後にその値を' name'に割り当てることができます。 – chepner

+0

これで最後に入力した株の正しい名前が印刷されるようになりましたが、すべての株が表示されるわけではありません。 3つの株を入力して3つの出力を得るために、コードを修正するにはどうすればよいですか? whileループの中ですべての計算を動かすと、前と同じ結果が出ました... –

0

ループを脱落した瞬間、 "-999"はストックの名前として保存された値なので、印刷されます。他の値は、 -999。

関連する問題