简体   繁体   English

如何调试此脚本,以便将数字替换为python中存在%的位置?

[英]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 您的代码中的一些问题

  • Assigment is done using the = operator, not the == operator which is used for comparison 使用=运算符而不是用于比较的==运算符进行赋值
  • The formatting variable will be inside the print statement and not outside it 格式变量将位于print语句内部,而不位于外部
  • You don't need to surround your variables around () 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 因此,更改后的代码将如下所示,请注意,该代码将同时适用于python2python3

#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来打印语句,再次与python2python3兼容,其中变量在字符串的{}内替换

#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.

相关问题 如何在python脚本中调试KCL(python)? - How can I debug KCL(python) in python script? 如何以交互方式调试python vim脚本? - How can I interactively debug a python vim script? 如何延迟php脚本的执行时间,以便python脚本可以完成 - How can I delay the execution time of a php script, so the python script can finish 我怎么知道我的python脚本挂在哪里? - How can I tell where my python script is hanging? 如何将python中的字符串转换为数字向量,以便我可以将它们与其他向量进行比较? - How to convert strings in python to vectors of numbers so i can compare them to other vectors? 我如何编写我的 python 代码,以便对 .txt 文件中的数字求和? - How can i write my python code so that it sums the numbers from a .txt file? 如何将一段文本转换为数字列表,以便“abc”在 python 中变为 [0,1,2](空格键变为 26)等 - How can I convert a piece of text into a list of numbers so that “abc” goes to [0, 1, 2] (with a spacebar going to 26) etc in python 我正在尝试使用shell命令在python中发送邮件。 但是变量不会在脚本中被替换 - I am trying to send mail in python using shell command . But variable does not get substituted in script 如何配置PyCharm,以便在Python控制台中可以看到正在运行的Python脚本文件 - how can I configure PyCharm so that running a Python script file is visible for Python Console 我已经在 selenium 中为 python 中的 odoo 框架创建了一个脚本,那么我可以将它用于负载测试吗? 如果是,那么如何? - I have created a script in selenium for odoo framework in python , so can I use it for a load testing ? if yes then how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM