Python 3.3 if / else / elif

时间:2017-08-31 12:16:19

标签: python if-statement python-3.3

我在if,else和elif语句中遇到了一些问题。问题是我无法弄清楚如何在代码之后获取代码来打印语句。

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

这是我目前所知道的代码。

535.00

据我所知,上面的代码是正确的,因为我可以让Python打印,它给了我if

但我无法确定elseelifif 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 命令出错的地方。

{{1}}

1 个答案:

答案 0 :(得分:1)

您需要正确的缩进和else语句

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

或者如果您只想要2个案例:

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

PS:更正了打印