[英]How can I debug this script so that numbers are substituted to where there is a % in python?
What am I doing wrong? 我究竟做错了什么?
I am trying to execute a basic code and I can't understand why the print function won't define the values with the '%' syntax. 我正在尝试执行基本代码,但我不明白为什么print函数不会使用'%'语法定义值。 Any suggestions on what I can do to improve would be greatly appreciated.
任何有关我可以做些什么的建议,将不胜感激。 I am using Python 3.
我正在使用Python 3。
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)
Some problems in your code 您的代码中的一些问题
=
operator, not the ==
operator which is used for comparison =
运算符而不是用于比较的==
运算符进行赋值 ()
to use your variables in formatting ()
即可在格式化中使用变量 Hence the changed code will be as follows, and note that it will work for both python2
and python3
因此,更改后的代码将如下所示,请注意,该代码将同时适用于
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)
The output will be 输出将是
There are 40 cars on the road.
There are 15 buses outside the school.
There are 30 at the swimming pool today.
Starting python3.7 we have a new way of formatting string using f-strings , where the variable name is replaced inside the {var}
从python3.7开始,我们提供了一种使用f-strings格式化字符串的新方法,其中变量名在
{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.")
We also have string.format to print statements, compatible for both python2
and python3
again, where the variables are replaced inside the {}
in the string 我们还有string.format来打印语句,再次与
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))
In python 2.7.15 (it could be other versions honestly probably is but I use this version) if you get rid of one of the equal signs it will work. 在python 2.7.15中(老实说它可能是其他版本,但我使用的是该版本),如果您摆脱了将起作用的等号之一。 Check it out below
在下面查看
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)
In python 3 and above you can do the following method * notice the minor differences in the print statements. 在python 3及更高版本中,您可以执行以下方法*请注意print语句中的细微差别。 Where you had an extra parentheses in the print statement.
打印语句中有多余括号的位置。 Also as per the comment by @Daniel Pryden you don't need the parentheses
同样根据@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)
Lastly the other method as mentioned in the comments is using the .format function. 最后,注释中提到的另一种方法是使用.format函数。 The .format() function is a method of the string which class allows you to do variable substitutions and value formatting.
.format()函数是字符串的一种方法,该类允许您进行变量替换和值格式化。 This lets you concatenate elements together within a string through positional formatting.
这使您可以通过位置格式将字符串内的元素连接在一起。 The definition above is from Digital Ocean the link is here
上面的定义来自Digital Ocean,链接在这里
people = 30
cars = 40
buses = 15
print("There are {} cars on the road.".format(cars))
print("There are {} buses outside the school.".format(buses))
print("There are {} at the swimming pool today.".format(people))
As was mentioned before, the =
operator is used for assignment statements . 如前所述,
=
运算符用于赋值语句 。 The ==
operator is used for value comparison expressions . ==
运算符用于值比较表达式 。
Regarding printing formatted output, in Python 3.6 or newer, the recommended approach is to use formatted string literals (aka f-strings) instead. 关于打印格式化的输出,在Python 3.6或更高版本中,建议的方法是改用格式化的字符串文字(也称为f字符串) 。
Putting this together, here is a corrected version for Python 3.6 or newer: 综上所述,这是Python 3.6或更高版本的更正版本:
people = 10
cars = 40
buses = 15
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.')
Some of typo errors I fixed: 我修正了一些错字错误:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.