0
我遇到了if,else和elif语句的问题。问题是我无法弄清楚如何让代码在代码之后打印语句。 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.')
以下是我知道的代码到目前为止的代码。
>>> 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)
至于我可以告诉上面的代码是正确的,因为我可以问Python来打印,这让我535.00
。
我不知道我在哪里出错了if
,else
或elif
命令。
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
为什么你是否缩进'else'和'elif'?你需要用'if'语句来保持这些级别。 –
另外,'else'不需要测试,只有'elif'。所以'else:'是有效的,否则利润<0:'不是。 –
下面的答案是否有帮助? – kenfire