2016-10-13 19 views
-2

Ich brauche das main(): um load(), calc() und display() in dieser Reihenfolge aufzurufen. Das Programm stoppt jedoch nach dem Laden. Die Ausgabe wird die Last nur ohne Berechnung oder Druck loopen.main() funktioniert nicht richtig

Ich wurde speziell angewiesen, calc() und display() NICHT im While-Loop-Block zu platzieren, was auch verlockend ist. Beachten Sie auch, dass das Problem gelöst wird, aber das ist nicht die Lösung, nach der ich gezielt suche.

Was muss ich tun, damit dieses Programm ordnungsgemäß funktioniert?

Ausgabe sollte wie folgt aussehen:

Enter stock name OR -999 to Quit: APPLE 
Enter number of shares: 10000 
Enter purchase price: 400 
Enter selling price: 800 
Enter commission: 0.04 

Stock Name: APPLE 
Amount paid for the stock:  $ 4,000,000.00 
Commission paid on the purchase: $ 160,000.00 
Amount the stock sold for:  $ 8,000,000.00 
Commission paid on the sale:  $ 320,000.00 
Profit (or loss if negative): $ 3,520,000.00 

Enter stock name OR -999 to Quit: FACEBOOK 
Enter number of shares: 10000 
Enter purchase price: 5 
Enter selling price: 500 
Enter commission: 0.04 

Stock Name: FACEBOOK 
Amount paid for the stock:  $ 50,000.00 
Commission paid on the purchase: $ 2,000.00 
Amount the stock sold for:  $ 5,000,000.00 
Commission paid on the sale:  $ 200,000.00 
Profit (or loss if negative): $ 4,748,000.00 

Enter stock name OR -999 to Quit: -999 
Total Profit is $ 14,260,000.00 

Hier ist die Ausgabe I LERNEN AM (dass ich nicht wollen):

====== RESTART: C:\Users\Elsa\Desktop\Homework 3, Problem 1.py ====== 
Enter stock name OR -999 to Quit: YAHOO! 
Enter number of shares: 10000 
Enter purchase price: 10 
Enter selling price: 100 
Enter commission: 0.04 

Enter stock name OR -999 to Quit: GOOGLE 
Enter number of shares: 10000 
Enter purchase price: 15 
Enter selling price: 150 
Enter commission: 0.03 

Enter stock name OR -999 to Quit: -999 

Stock Name: -999 
Amount paid for the stock:  $ 150,000.00 
Commission paid on the purchase: $ 4,500.00 
Amount the stock sold for:  $ 1,500,000.00 
Commission paid on the sale:  $ 45,000.00 
Profit (or loss if negative): $ 1,300,500.00 

Total Profit is $ 1,300,500.00 
>>> 
+0

"ich habe die Haupt benötigen():. Last() aufzurufen, calc() und Anzeige() in dieser Reihenfolge" - Und das passiert. Job ist erledigt, oder? "Das Programm stoppt jedoch nach dem Laden." - Nein. Nein, tut es nicht. Ihre Ausgabe zeigt deutlich, dass es sich um "calc" und "display" handelt. – user2357112

+1

Es klingt, als hätten Sie Ihre Projektanforderungen falsch verstanden. Da Sie uns Ihre Projektanforderungen nicht wirklich mitgeteilt haben, können wir nicht viel tun, um Ihnen zu helfen. – user2357112

+0

Unabhängig von meiner Anfänger Artikulation des Problems, ich möchte es wie die Ausgabe Sample aussehen, die ich zur Verfügung gestellt. Wie komme ich an diesen Punkt, ohne def Main zu berühren :()? – CreamSoda

Antwort

1

Ich denke, das ist die Lösung (einer von vielen) Sie könnten wahrscheinlich nehmen:

def load():  
    shares=int(input("Enter number of shares: ")) 
    pp=float(input("Enter purchase price: ")) 
    sp=float(input("Enter selling price: ")) 
    commission=float(input("Enter commission: ")) 
    return shares, pp, sp, commission 


def calc(totalpr, shares, pp, sp, commission): 
    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) 
    totalpr=totalpr+profit_loss 
    return (amount_paid, commission_paid_purchase, amount_sold, 
      commission_paid_sale, profit_loss, totalpr) 

def display(name, amount_paid, commission_paid_purchase, 
      amount_sold, commission_paid_sale, profit_loss): 
    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(): 
    totalpr = 0 
    name=input("Enter stock name OR -999 to Quit: ") 
    while name != '-999': 
     shares, pp, sp, commission = load() 
     am_paid, com_paid_purch, am_sold, \ 
     com_paid_sale, profit_loss, totalpr = calc(totalpr, shares, pp, sp, commission) 
     display(name, am_paid, com_paid_purch, 
       am_sold, com_paid_sale, profit_loss) 
     name=input("Enter stock name OR -999 to Quit: ") 
    return totalpr 
totalpr = main() 

print("\nTotal Profit is $", format(totalpr, '10,.2f'))