126 lines
5.9 KiB
Markdown
126 lines
5.9 KiB
Markdown
|
|
# 你的第一个 Python 程序
|
|||
|
|
|
|||
|
|
> 原文:[https://python . land/introduction-to-python/your-first-program](https://python.land/introduction-to-python/your-first-program)
|
|||
|
|
|
|||
|
|
如果你从一开始就遵循了 [Python 教程](https://python.land/python-tutorial),那么现在你已经学到了很多。我们已经讨论了关键主题,比如[布尔和条件编程](https://python.land/introduction-to-python/python-boolean-and-operators)、[字符串](https://python.land/introduction-to-python/strings)和[函数](https://python.land/introduction-to-python/functions)。我们还没有做的是创建一个实际的程序。所以,让我们总结一下,把我们学到的东西整合成一个漂亮的小程序。我们将一起创建你的第一个 Python 程序。
|
|||
|
|
|
|||
|
|
目录
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
* [在 REPL 中输入代码](#Entering_the_code_in_the_REPL "Entering the code in the REPL")
|
|||
|
|
* [分析你的第一个 Python 程序](#Analyzing_your_first_Python_program "Analyzing your first Python program")
|
|||
|
|
* 任务:改编你的第一个 Python 程序
|
|||
|
|
* 现在呢?
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
## 在 REPL 中输入代码
|
|||
|
|
|
|||
|
|
我先分享一下节目。请彻底分析后再继续阅读。有一个你还不知道的函数(input ),但是我将很快解释它:
|
|||
|
|
|
|||
|
|
```py
|
|||
|
|
def say_hi(name):
|
|||
|
|
if name == '':
|
|||
|
|
print("You didn't enter your name!")
|
|||
|
|
else:
|
|||
|
|
print("Hi there...")
|
|||
|
|
for letter in name:
|
|||
|
|
print(letter)
|
|||
|
|
|
|||
|
|
name = input("Your name: ")
|
|||
|
|
say_hi(name)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
当输入超过几行代码时,必须非常小心缩进。如果您因为格式或缩进而不断得到错误,您也可以尝试完全复制并粘贴代码。在 REPL,它应该看起来像下面这样:
|
|||
|
|
|
|||
|
|
```py
|
|||
|
|
>>> def say_hi(name):
|
|||
|
|
... if name == '':
|
|||
|
|
... print("You didn't enter your name!")
|
|||
|
|
... else:
|
|||
|
|
... print("Hi there...")
|
|||
|
|
... for letter in name:
|
|||
|
|
... print(letter)
|
|||
|
|
...
|
|||
|
|
>>> name = input("Your name: ")
|
|||
|
|
< enter your name at this point >
|
|||
|
|
>>> say_hi(name)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
如果你还有问题,这里有我的[在线 Python 解释器](https://python.land/installing-python/python-in-the-browser)的一个版本,你可以直接从这个页面运行。注意,`input()`在这个系统中不起作用,所以我们只是用一个预定义的字符串调用`say_hi()`:
|
|||
|
|
|
|||
|
|
[https://crumb . sh/embed/w75 zob 5 wmuh](https://crumb.sh/embed/W75zoB5WmuH)
|
|||
|
|
|
|||
|
|
你的第一个 Python 程序
|
|||
|
|
|
|||
|
|
## 分析你的第一个 Python 程序
|
|||
|
|
|
|||
|
|
让我们一步一步地检查代码。
|
|||
|
|
|
|||
|
|
### 使用 Python 请求输入
|
|||
|
|
|
|||
|
|
我设法塞进了一个新东西,内置函数`input()`。它完全按照您的期望去做:请求输入并将输入赋给一个变量。如果你给`input`一个字符串作为参数,它会把它作为前缀打印出来。在这种情况下,它将打印“您的姓名:”并等待您输入您的姓名。
|
|||
|
|
|
|||
|
|
### 带有一个参数的 say_hi 函数
|
|||
|
|
|
|||
|
|
我们定义的`say_hi(name)` [Python 函数](https://python.land/introduction-to-python/functions)接受一个参数,即名称,并将名称打印到屏幕上。该函数不返回任何内容。它不需要,因为它自己完成所有的打印工作。
|
|||
|
|
|
|||
|
|
### 如果..else 块
|
|||
|
|
|
|||
|
|
我们的函数只在名字不是空字符串时才通知我们。为什么会这样?
|
|||
|
|
|
|||
|
|
Thank you for reading my tutorials. I write these in my free time, and it requires a lot of time and effort. I use ads to keep writing these *free* articles, I hope you understand! **Support me by disabling your adblocker on my website** or, alternatively, **[buy me some coffee](https://www.buymeacoffee.com/pythonland)**. It's much appreciated and allows me to keep working on this site!
|
|||
|
|
|
|||
|
|
当有人被要求输入时点击 enter 键,`input()`函数返回一个空字符串。你可以在 REPL 亲自查看:
|
|||
|
|
|
|||
|
|
```py
|
|||
|
|
>>> input()
|
|||
|
|
''
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
当要求输入时,只需按 enter,您将看到对 input()的调用产生一个空字符串。所以为了不让自己出丑,我们不会问候表现出如此粗鲁行为的人。我们通过使用一个 [if-else 块](https://python.land/introduction-to-python/python-boolean-and-operators)来检查输入是否等于一个空字符串。
|
|||
|
|
|
|||
|
|
### for 循环
|
|||
|
|
|
|||
|
|
最后,使用 [Python 的 for 循环](https://python.land/introduction-to-python/python-for-loop),我们将输入姓名的每个字母打印在新的一行上,因为我们可以。
|
|||
|
|
|
|||
|
|
## 任务:改编你的第一个 Python 程序
|
|||
|
|
|
|||
|
|
由于我们定义了一个函数`say_hi(name)`,我们可以重用这个函数。可以反复问名字,反复打电话 say_hi。这里有一个小任务:
|
|||
|
|
|
|||
|
|
```py
|
|||
|
|
Create an infinite loop that keeps asking for names,
|
|||
|
|
and that keeps greeting us using the entered name.
|
|||
|
|
|
|||
|
|
Hint 1: use the say_hi function from above.
|
|||
|
|
Hint 2: revisit the section about loops if you need to.
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
```py
|
|||
|
|
def say_hi(name):
|
|||
|
|
if name == '':
|
|||
|
|
print("You didn't enter your name!")
|
|||
|
|
else:
|
|||
|
|
print('Hi there...')
|
|||
|
|
|
|||
|
|
for letter in name:
|
|||
|
|
print(letter)
|
|||
|
|
|
|||
|
|
# This is an infinite loop
|
|||
|
|
while True:
|
|||
|
|
# Ask for the name first using input()
|
|||
|
|
name = input('Your name: ')
|
|||
|
|
# And then call say_hi with that name
|
|||
|
|
say_hi(name)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 现在呢?
|
|||
|
|
|
|||
|
|
此时,使用交互式 Python shell 开始对我们不利。您可能已经做了很多工作来让第一个 Python 程序工作,主要是因为缩进问题。幸运的是,我们还可以将 Python 程序存储在文件中,您将在下一节中了解到:[创建 Python 程序](https://python.land/creating-python-programs)。但是在我们这样做之前,我们将首先深入了解一下 [Python 注释](https://python.land/introduction-to-python/python-comment)。
|
|||
|
|
|
|||
|
|
目前:恭喜你。如果您一直在学习,那么您应该对使用 Python 编程有一个基本的了解。您创建了您的第一个 Python 程序!我建议你继续在 REPL 里面做实验。您可能需要重读部分或全部章节。这是正常的。
|
|||
|
|
|
|||
|
|
在这一点上,我想给出的最重要的一条建议是,你不能光靠阅读来学习编程,就像你不能光靠阅读来成为一名医生一样。你必须亲自动手练习。
|
|||
|
|
|
|||
|
|
如果你觉得准备好了,继续下一章!
|