如何调试此脚本,以便在python中有%的位置替换数字?
我做错了什么 我正在尝试执行一个基本代码,我不明白为什么print函数不使用“%”语法定义值。任何关于我能做些什么来改进的建议都将不胜感激。我正在使用Python3如何调试此脚本,以便在python中有%的位置替换数字?,python,Python,我做错了什么 我正在尝试执行一个基本代码,我不明白为什么print函数不使用“%”语法定义值。任何关于我能做些什么来改进的建议都将不胜感激。我正在使用Python3 people == 30 cars == 40 buses == 15 print ("There are %s cars on the road.") % (cars) print ("There are %s buses outside the school.") % (buses) print ("There are %s
people == 30
cars == 40
buses == 15
print ("There are %s cars on the road.") % (cars)
print ("There are %s buses outside the school.") % (buses)
print ("There are %s at the swimming pool today.") % (people)
我修复了一些打字错误:
people = 30
cars = 40
buses =15
print ("There are %s cars on the road." % cars)
print ("There are %s buses outside the school." % buses)
print ("There are %s at the swimming pool today." % people)
在Python2.7.15中(可能是其他版本,老实说可能是,但我使用这个版本),如果去掉其中一个等号,它将起作用。看看下面
people = 30
cars = 40
buses = 15
print("There are %s cars on the road.") % (cars)
print("There are %s buses outside the school.") % (buses)
print("There are %s at the swimming pool today.") % (people)
在Python3及以上版本中,您可以执行以下方法*注意print语句中的细微差异。在print语句中有一个额外的括号。根据@Daniel Pryden的评论,您不需要括号
people = 30
cars = 40
buses = 15
print("There are %s cars on the road." % cars)
print("There are %s buses outside the school." % buses)
print("There are %s at the swimming pool today." % people)
最后,注释中提到的另一种方法是使用.format函数。.format()函数是字符串的一种方法,该类允许您执行变量替换和值格式化。这使您可以通过位置格式将字符串中的元素连接在一起。上面的定义来自Digital Ocean,链接是
代码中的一些问题
- 分配是使用
运算符完成的,而不是用于比较的=
运算符=
- 格式化变量将位于print语句内部,而不是外部
- 在格式化中使用变量时,不需要将变量包围在
周围()
python2
和python3
#Use assignment operator
people = 30
cars = 40
buses = 15
#Use variables inside the print statement, which replaces the formatting operator
print("There are %s cars on the road." % cars)
print("There are %s buses outside the school." % buses)
print("There are %s at the swimming pool today." % people)
输出将是
There are 40 cars on the road.
There are 15 buses outside the school.
There are 30 at the swimming pool today.
从python3.7开始,我们有了一种新的格式化字符串的方法,即在{var}
#Use assignment operator
people = 30
cars = 40
buses = 15
#Use variables inside the f-strings
print(f"There are {cars} cars on the road.")
print(f"There are {buses} buses outside the school.")
print(f"There are {people} at the swimming pool today.")
我们还必须打印语句,再次兼容python2
和python3
,其中变量在字符串的{}
中被替换
#Use assignment operator
people = 30
cars = 40
buses = 15
#Use format strings
print("There are {} cars on the road.".format(cars))
print("There are {} buses outside the school.".format(cars))
print("There are {} at the swimming pool today.".format(buses))
如前所述,
=
运算符用于。=
运算符用于
关于打印格式化输出,在Python3.6或更新版本中,建议改为使用
综上所述,以下是Python 3.6或更新版本的更正版本:
people=10
汽车=40
公共汽车=15辆
打印(f'路上有{cars}辆车')
打印(f‘校外有{公共汽车}辆公共汽车’)
打印(f‘今天游泳池里有{人}’)
请给我们看一下代码。请给我们看一下代码好吗没有实际代码,这个问题与堆栈溢出无关。但是我怀疑你能找到你想要的答案。你展示的代码在Python2.x中是有效的语法,但在Python3和更新版本中是无效的语法(其中,print
是一个函数而不是一个语句)。您使用的是什么版本的Python?@DanielPryden它在Python3中实际上是有效的语法,但它将被解析为函数调用和模运算。。。由于print
返回None
,因此在运行时会导致TypeError
。。。(对于OP,在python3中,为了正确地进行字符串插值,必须将(cars)
、(bus)
或(people)
放在字符串后面的括号内)。
#Use assignment operator
people = 30
cars = 40
buses = 15
#Use format strings
print("There are {} cars on the road.".format(cars))
print("There are {} buses outside the school.".format(cars))
print("There are {} at the swimming pool today.".format(buses))