简体   繁体   English

SyntaxError:打印中的语法无效 function

[英]SyntaxError: Invalid syntax in print function

I'm working on the below script on my MacbookAir and I'm unclear where this syntax error comes from and tried searching on google why it breaks at the = sign in the print function.我正在我的 MacbookAir 上处理以下脚本,我不清楚这个语法错误来自哪里,并尝试在谷歌上搜索为什么它在打印 function 中的 = 符号处中断。

I understood there are different functions to print and tried many of them.我了解有不同的打印功能并尝试了其中的许多功能。 But am unclear if I'm using the correct Python version (both 2 and 3 are installed).但我不清楚我是否使用了正确的 Python 版本(安装了 2 和 3)。

Can you please help out?你能帮忙吗?

I get an error in line 61 :我在第61行收到错误:

print("The interest rate is too high to trade {}".format(total_profit) , end="\n", file=output_file)

Script:脚本:

## Initial values from the Python script 
principal=1000
coupon=0.06
frequency=2
r=0.03
transaction_fee = 0.003*principal

## Amendments to the variables as per question 7
probabilitypayingout=0.85
probabolilitynotpayingout=0.15
notpayingoutfullamount=200
maturity=7
market_price=1070
avoidtradingaboveinterestrate=0.02 

#!/usr/bin/env python3.7
import numpy as np

# Open a file to store output
output_file = open("outputfile.txt", "w")

# print variables of this case
print("The variables used for this calculation are: \n - Probability of paying out the full principal {}.".format(probabilitypayingout), "\n - Probability of paying out partial principal {}.".format(probabolilitynotpayingout), "\n - Amount in case of paying out partial principal {}.".format(notpayingoutfullamount), "\n - Market price bond {}.".format(market_price), "\n - Bond maturity in years {}.".format(maturity), "\n - Coupon rate bond {}.".format(coupon), "\n - Principal bond {}.".format(principal), "\n - Frequency coupon bond {}.".format(frequency) , "\n - Risk free rate {}.".format(r) , "\n - Avoid trading aboe interest rate {}.".format(avoidtradingaboveinterestrate), "\n \n"   )

# calculate true value and decide whether to trade     

true_price=0
principalpayout=(probabilitypayingout*principal)+(probabolilitynotpayingout*notpayingoutfullamount)

for t in range(1,maturity*frequency+1):
  if t<(maturity*frequency):
    true_price = true_price + (coupon/frequency)*principal/(1+(r/frequency))**t  # Present value of coupon payments
  else:
    true_price = true_price + (coupon/frequency)*principal/(1+(r/frequency))**t + principalpayout/(1+(r/frequency))**t  # Present value of coupons and principal

print("The price of the bond according to the pricing model is {}, while the current market price is {}.".format(true_price, market_price)) 

if true_price-transaction_fee>market_price:
  profit = true_price-transaction_fee-market_price
  print("The trade is executed and if the pricing model is correct, the profit will be {}".format(profit), "after deduction of trading fees.")  
else:
  print("The trade was not executed, because the expected profit after transaction fees is negative.")  

# Fifth, mimic changes in market conditions by adjusting the interest rate and market price. The indented code below the "for" line is repeated 1,000 times. 
total_profit=0

for n in range(0,1000):
  # Adds some random noise to the interest rate and market price, so each changes slightly (each time code is executed, values will differ because they are random)
  change_r=np.random.normal(0,0.015)  
  change_market_price=np.random.normal(0,40) 
  r_new = r + change_r
  market_price_new = market_price + change_market_price

  # Sixth, execute trading algorithm using new values  
  true_price_new=0


if r_new>avoidtradingaboveinterestrate:


    print("The interest rate is too high to trade {}".format(total_profit) , end="\n", file=output_file)
    output_file.close()

else:
    for t in range(1,maturity*frequency+1):
        if t<(maturity*frequency):
          true_price_new = true_price_new + (coupon/frequency)*principal/(1+(r_new/frequency))**t
        else:
          true_price_new = true_price_new + (coupon/frequency)*principal/(1+(r_new/frequency))**t + principalpayout/(1+(r_new/frequency))**t

    if true_price_new-transaction_fee>market_price_new:
        trading_profit = true_price_new-transaction_fee-market_price_new
        total_profit = total_profit + trading_profit
      print("The trade was executed and is expected to yield a profit of {}. The total profit from trading is {}.".format(trading_profit,total_profit), end="\n", file=output_file    )

print("The total profit from trading is {}".format(total_profit), end="\n", file=output_file)
output_file.close()

print("The interest rate is too high to trade {}".format(total_profit), end="\n", file=output_file) print("利率太高无法交易{}".format(total_profit), end="\n", file=output_file)

Is a Python 3 syntax.Python 3语法。 Run python3 your_file.py instead of python your_file.py .运行python3 your_file.py your_file.py 而不是python your_file.py

I think you're not using the correct python version.我认为您没有使用正确的 python 版本。 The default python version on mac os is still python 2.X to use python 3.x you have to call the interpreter with "python3". mac os 上的默认 python 版本仍然是 python 2.X 要使用 python 3.x,您必须使用“python3”调用解释器。 Try this command:试试这个命令:

python3 PythonMIFIAT.py python3 PythonMIFIAT.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM