2017-08-31 4 views
0

Ich habe ein Problem mit den Anweisungen if, else und elif. Das Problem ist, ich kann nicht herausfinden, wie ich den Code eine Anweisung nach dem Code drucken kann. Python 3.3 if/else/elif

if profit > 0: 
    print('After the transaction, you lost ' ,profit, ' dollars.') 
elif profit < 0: 
    print('After the transaction, you gained ' ,profit, ' dollars.') 

Hier ist der Code, den ich Werke bisher kennen.

>>> shares=float(input("Enter number of shares:")) 
Enter number of shares:2000 
>>> price1=float(input("Enter purchase price:")) 
Enter purchase price:40 
>>> price2=float(input("Enter sale price:")) 
Enter sale price:42.75 
>>> profit= 0.97 * (price2 * shares) - 1.03 * (price1 * shares) 

Soweit ich über den Code sagen kann, ist korrekt, weil ich Python fragen zu drucken und es gibt mir 535.00.

ich herausfinden, kann aber nicht, wo ich falsch gehe mit dem if, else oder elif Befehl.

if profit > 0: 
    print('After the transaction, you lost ' ,profit, 'dollars.') 
    else profit < 0: 

SyntaxError: invalid syntax 

if profit > 0: 
    print('After the transaction, you lost ' ,profit, 'dollars.') 
    else profit < 0: 

SyntaxError: invalid syntax 

if profit > 0: 
    print('After the transaction, you lost' ,profit, 'dollars.') 
    elif profit < 0: 

SyntaxError: invalid syntax 
+2

Warum Ziehen Sie "else" und "elif" ein? Sie müssen diese Ebene mit der 'if'-Anweisung beibehalten. –

+1

Auch "else" nimmt keinen Test, nur 'elif' tut. Also 'sonst:' ist gültig, 'sonst ist Profit <0:' nicht. –

+0

Hilft die folgende Antwort? – kenfire

Antwort

1

Sie benötigen einen richtigen Gedankenstrich und eine andere Aussage

if profit > 0: 
    print('After the transaction, you gained ', profit, ' dollars.') 
elif profit < 0: 
    [code...] 
else: 
    [code...] 

Oder wenn Sie nur 2 Fälle:

if profit > 0: 
    print('After the transaction, you gained ', profit, ' dollars.') 
else: 
    print('After the transaction, you lost ', -profit, 'dollars.') 

PS: Korrigiert die Druck