2016-10-12 6 views
0
def load():  
    global name 
    global count 
    global shares 
    global pp 
    global sp 
    global commission 
    name=input("Enter stock name OR -999 to Quit: ") 
    count =0 
    while name != '-999': 
     count=count+1 
     shares=int(input("Enter number of shares: ")) 
     pp=float(input("Enter purchase price: ")) 
     sp=float(input("Enter selling price: ")) 
     commission=float(input("Enter commission: ")) 
     calc() 
     display() 
     name=input("\nEnter stock name OR -999 to Quit: ") 

def calc(): 
    global amount_paid 
    global amount_sold 
    global profit_loss 
    global commission_paid_sale 
    global commission_paid_purchase 
    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) 

def display(): 
    print("\nStock 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')) 

def main(): 
    load() 
    calc() 
    display() 

main() 

センチネルは、プログラムを終了する意味では機能しますが、以前の株のデータ(誤っている)は出力されません。私は、センチネルが直ちにプログラムを停止し、 '-999'を入力した後に「>>>」にキックするようにしたいと思います。 main()からcalc()display()を削除センチネルが正しく機能していない

============= RESTART: C:\Users\ElsaTime\Desktop\answer2.py ============= 
Enter stock name OR -999 to Quit: M$FT 
Enter number of shares: 1000 
Enter purchase price: 15 
Enter selling price: 150 
Enter commission: 0.03 

Stock Name: M$FT 
Amount paid for the stock:  $ 15,000.00 
Commission paid on the purchase: $  450.00 
Amount the stock sold for:  $ 150,000.00 
Commission paid on the sale:  $ 4,500.00 
Profit (or loss if negative): $ 130,050.00 

Enter stock name OR -999 to Quit: -999 

Stock Name: -999 
Amount paid for the stock:  $ 15,000.00 
Commission paid on the purchase: $  450.00 
Amount the stock sold for:  $ 150,000.00 
Commission paid on the sale:  $ 4,500.00 
Profit (or loss if negative): $ 130,050.00 
>>> 

答えて

1

:ここでは、出力は次のようになります。あなたはすでにload()でこれらのことをしています。

関連する問題