Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的净损益_Python - Fatal编程技术网

python中的净损益

python中的净损益,python,Python,我正在做一个项目,我需要弄清楚我的交易是会给我带来利润还是会给我带来损失。这是基于股票买卖的佣金。我把基本的数学概念都算出来了。我正在努力解决的是最后一个等式,即从购买价格中减去销售价格。我需要一个(或两个)声明,如果答案是肯定的,它将转到输出,说我获得了x金额的钱。如果答案是否定的,它将转到输出,说我损失了x笔钱 在Python程序知道如何发送正数以获得输出,以及如何发送负数以丢失输出的地方,我可以使用哪些语句 下面是我编写的代码,以及您对if语句的建议 number_of_shares =

我正在做一个项目,我需要弄清楚我的交易是会给我带来利润还是会给我带来损失。这是基于股票买卖的佣金。我把基本的数学概念都算出来了。我正在努力解决的是最后一个等式,即从购买价格中减去销售价格。我需要一个(或两个)声明,如果答案是肯定的,它将转到输出,说我获得了x金额的钱。如果答案是否定的,它将转到输出,说我损失了x笔钱

在Python程序知道如何发送正数以获得输出,以及如何发送负数以丢失输出的地方,我可以使用哪些语句

下面是我编写的代码,以及您对if语句的建议

number_of_shares = input("Enter number of shares: ")
purchase_price = input("Enter purchase price: ")
sale_price = input("Enter sale price: ")

price = number_of_shares * purchase_price
first_commission = price * .03
final_price = price - first_commission

sale = number_of_shares * sale_price
second_commission = sale * .03
last_price = sale - second_commission

net = final_price - last_price
if (net > 0):
print("After the transaction, you gained", net, "dollars.")
if (net < 0):
print("After the transaction, you lost", net, "dollars.")
number of_shares=输入(“输入股份数量:”)
采购价格=输入(“输入采购价格:”)
销售价格=输入(“输入销售价格:”)
价格=股份数量*购买价格
第一次佣金=价格*.03
最终价格=价格-首次佣金
销售=股份数量*销售价格
二次佣金=销售额*.03
最后价格=销售-第二次佣金
净=最终价格-最后价格
如果(净值>0):
打印(“交易后,您获得”,净额,“美元”)
如果(净<0):
打印(“交易后,您损失了”,净额,“美元”)
我没有意识到我把损失当成了收益,反之亦然,所以我把它换了个地方,改变了措辞,让它更清楚。我还是被困在这里这是我的更新代码

number_of_shares = input("Enter number of shares: ")
purchase_price = input("Enter purchase price: ")
sale_price = input("Enter sale price: ")

price = number_of_shares * purchase_price
first_commission = price * .03
buy_price = price - first_commission

sale = number_of_shares * sale_price
second_commission = sale * .03
sell_price = sale - second_commission

net = sell_price - buy_price
if net > 0:
    print("After the transaction, you gained", net, "dollars.")
if net < 0:
    print("After the transaction, you lost", net, "dollars.")
number of_shares=输入(“输入股份数量:”)
采购价格=输入(“输入采购价格:”)
销售价格=输入(“输入销售价格:”)
价格=股份数量*购买价格
第一次佣金=价格*.03
购买价格=价格-优先佣金
销售=股份数量*销售价格
二次佣金=销售额*.03
售价=销售额-第二次佣金
净=卖出价-买入价
如果网络>0:
打印(“交易后,您获得”,净额,“美元”)
如果净值<0:
打印(“交易后,您损失了”,净额,“美元”)
在纸上完成代码后,我发现了我的错误(与委员会一起),并进行了更改。现在我的问题是,当净值为亏损时,产出给了我一个负数。我怎样才能使它不是负的?因为我已经有了声明-你损失了x美元。嗯,乘以负1?这就是我所做的

number_of_shares = int(input("Enter number of shares: "))
purchase_price = float(input("Enter purchase price: "))
sale_price = float(input("Enter sale price: "))

buy = float(number_of_shares * purchase_price)
first_commission = float(buy * .03)

sale = float(number_of_shares * sale_price)
second_commission = float(sale * .03)

net = float(sale - buy - first_commission - second_commission)
if net > 0:
    print("After the transaction, you gained", net, "dollars.")
if net < 0:
    print("After the transaction, you lost", net * -1, "dollars.")
numberofshares=int(输入(“输入共享数量:”)
采购价格=浮动(输入(“输入采购价格:”)
销售价格=浮动(输入(“输入销售价格:”)
购买=浮动(股份数量*购买价格)
首次佣金=浮动(买入*.03)
销售=浮动(股份数量*销售价格)
二次佣金=浮动(销售额*.03)
净=浮动(销售-购买-第一次佣金-第二次佣金)
如果网络>0:
打印(“交易后,您获得”,净额,“美元”)
如果净值<0:
打印(“交易后,您损失了”,净额*-1,“美元”)

如果看不到您目前掌握的代码,就很难为您提供帮助,但无论如何我都会尝试

听起来你在寻找一个
if
语句。以下内容可能会满足您的要求:

# Assuming total_money is the output of everything before
# positive number is gain, negative is loss
if total_money > 0:
    print 'You gained ${}'.format(total_money)
else:
    print 'You lost ${}'.format(total_money)

这是流量控制概念的一部分。要了解更多信息,您可以查看

尝试以下格式:

net_change = sell_price - buy_price - commission
if (net_change >0):
    print('Money! Money in the bank!\nYou have made $'+str(net_change))
else if (net_change <0):
    print('Oh, sad deleterious day!\nYou have lost $'+str(net_change))
else:
    print('You did not accomplish anything. You have broken even.')
净变动=卖出价-买入价-佣金 如果(净变化>0): 打印('钱!银行里的钱!\n您已经赚了$'+str(净变化))
否则,如果(净变化以下是有效的代码

number_of_shares = int(input("Enter number of shares:"))
purchase_price = float(input("Enter purchase price:"))
sale_price = float(input("Enter sale price:"))

buy = float(number_of_shares * purchase_price)
first_commission = float(buy * .03)

sale = float(number_of_shares * sale_price)
second_commission = float(sale * .03)

net = float(sale - buy - first_commission - second_commission)
if net > 0:
    print("After the transaction, you gained", net, "dollars.")
if net < 0:
    print("After the transaction, you lost", net * -1, "dollars.")
numberofshares=int(输入(“输入共享数量:”)
采购价格=浮动(输入(“输入采购价格:”)
销售价格=浮动(输入(“输入销售价格:”)
购买=浮动(股份数量*购买价格)
首次佣金=浮动(买入*.03)
销售=浮动(股份数量*销售价格)
二次佣金=浮动(销售额*.03)
净=浮动(销售-购买-第一次佣金-第二次佣金)
如果网络>0:
打印(“交易后,您获得”,净额,“美元”)
如果净值<0:
打印(“交易后,您损失了”,净额*-1,“美元”)

提示:小于或大于是的,我已经想到了,但直到看到另一个人的if声明建议,我才知道如何合并。我会尝试0本身不是一个“损失”。嗯!让我试试!作业不允许中性结果。但我刚刚注意到str部分(净费用)。我将尝试它确实允许中性结果,如果没有收益或损失,则调用最后的
else
语句,因此如果(净变化==0):