Michael F
Michael F

Reputation: 21

Python 3.3 if / else / elif

I am having some problem with the if, else, and elif statements. The problem is I cannot figure out how I can get the code to print a statement after the code.

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

Here is the code that I know works so far.

>>> 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)

As far as I can tell the code above is correct because I can ask Python to print and it gives me 535.00.

I can't however figure out where I am going wrong with the if, else, or elif command.

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

Upvotes: 1

Views: 691

Answers (1)

kenfire
kenfire

Reputation: 1355

You need a correct indent and an else statement

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

Or if you just want 2 cases:

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

PS: Corrected the print

Upvotes: 1

Related Questions