[英]Code in Jupyter Notebook is not running in Python 2.7.18
I wrote a function that was running on the Jupyter Notebook.我编写了一个在 Jupyter Notebook 上运行的函数。 Now I want to run the same code in Python 2.7.18 but getting a Syntax error in "" part..现在我想在 Python 2.7.18 中运行相同的代码,但在“”部分出现语法错误..
import datetime
now = datetime.datetime.now()
i = now.year
j = now.month
k = now.day
def concat(i, j):
return eval(f"{i}{j}{k}")
Use format
alternative instead f-string
s working in python 3.6+
:使用format
替代而不是f-string
在python 3.6+
工作:
def concat(i, j):
return eval("{}{}{}".format(i, j, k))
You're using f-strings here in python2, but this feature was introduced in python3.6你在 python2 中使用 f-strings,但是这个特性是在 python3.6 中引入的return eval(f"{i}{j}{k}")
You can format the output normally, or you can use the f-string emulator package for python 2: https://pypi.org/project/fstrings/您可以正常格式化输出,也可以使用python 2的f-string模拟器包: https : //pypi.org/project/fstrings/
f string only runs on Python 3.6+. f 字符串仅在 Python 3.6+ 上运行。 If you really need to run on Python 2.7.18 maybe you need to change your string use .format or using the % operator如果您确实需要在 Python 2.7.18 上运行,也许您需要使用 .format 或使用 % 运算符更改字符串
instead of代替
eval(f"{i}{j}{k}")
use用
eval("{0}{1}{2}".format(i,j,k))
or the oldest或最古老的
eval("%s %s %s" % (i, j ,k)")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.