ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

5/44 使用Print输出 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

5/44 使用Print输出

Using Print | Python for Beginners [5 of 44]

Okay. Let’s get into some coding.
好了 让我们开始学习一些代码吧
We’re going to start off with the print statement,
我们将从print语句开始学习
which is typically where we start with most programming languages.
一般来讲 学习大多数编程语言都会从这里开始
When you want to print output to the console to display a message
当你想要在控制台上显示你的消息
either to a user or to yourself maybe because you’re debugging your code,
还有其他用户或者你自己想要调试代码的时候
we use the print statement in Python.
我们就可以使用python中的print语句
It’s simply the command print,
只需先打上print命令
then in parentheses the string that or the message
把你想要显示的字符串或者消息
that you want to display.
括在括号里面就可以了
You can just contain it in single quotes
你既可以把字符串用单引号括起来
or it will also work inside double quotes.
也可以用双引号括起来
It’s one of the unusual things about Python is you can choose
Python与众不同的一点就是
either single quotes or double quotes to enclose your strings.
既可以用单引号又可以用双引号
Now, just because you can use either one
虽然你可以使用任何一种形式
doesn’t mean you should go back and forth.
但这并不意味着你应该来回切换
Pick one and stick to it.
选择一个一直用下去
Throughout this course, we’re actually
在这个课程里
always going to use single quote to enclose our strings.
我们一直会用单引号把字符串括起来
That’s a conscious choice we have made.
我们是有意为之
So we’re consistent
这样才能保持一致
and doesn’t confuse people that half the demos are one half with the other.
不会变来变去而让人感到困惑
You can choose what you want but please stay consistent.
你可以选一个你喜欢的形式 但请一定保持一致
Always use double quotes or always use single quotes.
只用双引号或者只用单引号
It is nice to know you can use either
但是了解多一点对你有好处
because you might find examples online,
否则当你在网上找到一些示例
if its switch back and forth or use when you’re not familiar with.
他们如果来回切换的话你可能会觉得很奇怪
Another thing you might need to do from time to time
还有一件你会经常做的事情
is actually ask for information from the user
就是当你运行一个项目的时候
when you are running a program.
需要询问用户的信息
We do that by using the input function.
这个我们可以通过input函数实现
So with the input function,
对于input函数
we pass in a message that we would display like,
当我们想要显示一个消息
“Hey, please enter your name,”
比如 “你好 请输入你的名字”
and whatever value is passed back by the user
无论用户输入的是什么
whenever they type into the screen
无论用户何时输入屏幕中
will then be put into that name variables.
这些信息都会赋值给变量name
So it’ll be stored in the variable name
然后它们会被储存在name变量中
and later on, we can then print that name out on the screen
之后 我们可以把name在屏幕上输出
or use it in our code as we need to.
或者根据需要在代码中使用
When you’re displaying output on the screen,
当你把输出结果显示在屏幕上的时候
it can very quickly become quite cluttered.
他们很容易聚集在一起
So it’s useful to insert blank lines in the output
所以为了让结果更容易阅读
to make it a little more readable.
在输出结果中插入空行会很有用
There’s a couple of ways we can display blank lines.
我们可以通过几种方式来插入空行
One, we can just say print
第一种 我们直接用print语句
and provide open and closed parentheses with nothing inside.
后面跟着一个完整的空括号
That we’ll print a blank line.
这样就可以显示一行空行
Also each print statement does put a blank line afterwards.
此外 每个print后面都会跟一个空行
So if you have a print (’Hello’)
所以如果你写一句print (‘Hello’)
and then you say, print (‘world’)
再写一句print (‘world’)
that would actually put a blank line between hello and world.
hello和world之间就会插入一行空行
Also, you can put a \n.
或者你可以加一个\n
That’s actually a special character
这其实是个特殊字符
that means, “Insert a new line here.”
意思是“在这里插入空行”
What that will do
这么做的结果是
is that will allow you to put a blank line
当你需要的时候
right in the middle of the string if you need it.
你可以在字符串中插入空行
One of the reasons print statements are really useful to coding is
print语句在编程中十分重要的原因是
because we can use them for debugging.
这个命令可以用来调试代码
Sometimes when we’re writing code,
当我们正在写代码的时候
an error message pops up
如果程序报错
or the codes just not doing what we think it should do.
或者代码无法按照预期正常运行
So sometimes we’ll add print statements
这时我们会添加print语句
not because they actually make the code work
不是因为这样可以让代码正常运行
but we write print statements so that when we’re running the code
而是使用print语句可以在代码运行过程中
and we’re trying to figure out where it’s blowing up
试着找出哪里出了问题
to see which lines of code are executing successfully
从而知道哪几行代码是是对的
and where it’s failing.
哪几行代码出了问题
So in this case,
所以在这种情况下
you can see that I saw the message “Adding numbers“ displayed.
你可以看到消息”Adding numbers“显示出来了
Then hopefully, it did the addition.
接下来 希望它已经完成了加法计算
Then I see the message,
接下来的消息是
I’m performing the division
我正在计算除法
and then hopefully it does division,
我们也希望它可以完成除法计算
and then I see this error message spewing all over the screen.
然后屏幕上开始报错
That can help me figure out where my code blew up.
这就能帮我找到我的代码到底是哪里出了问题
So now I have a sense since I never saw the message math complete,
所以现在我意识到”Math message“这个消息从未出现
I know where to go look at my code for the problem.
我就能知道代码中的问题出在哪儿
So it’s a useful tool
所以添加或者删减print语句在代码调试中
when debugging code to add and remove print statements.
十分有用
Just so you can figure out what’s going on.
这样你就可以知道发生了什么
All right. Now let’s go take a look at this inside Visual Studio Code.
好了 现在我们尝试把它放在Visual Studio Code中运行

发表评论

译制信息
视频概述

写代码第一步,输入和输出语句

听录译者

收集自网络

翻译译者

一颗大橙子

审核员

审核员 EM

视频来源

https://www.youtube.com/watch?v=FhoASwgvZHk

相关推荐