Skip to content

Commit

Permalink
modify
Browse files Browse the repository at this point in the history
  • Loading branch information
WuShichao committed Oct 5, 2018
1 parent b3ff450 commit 873f26b
Showing 1 changed file with 111 additions and 15 deletions.
126 changes: 111 additions & 15 deletions 06.control.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,46 @@

案例(保存为 `if.py`):

```text
{% include "./programs/if.py" %}
```python
number = 23
guess = int(input('Enter an integer : '))

if guess == number:
# 新块从这里开始
print('Congratulations, you guessed it.')
print('(but you do not win any prizes!)')
# 新块在这里结束
elif guess < number:
# 另一代码块
print('No, it is a little higher than that')
# 你可以在此做任何你希望在该代码块内进行的事情
else:
print('No, it is a little lower than that')
# 你必须通过猜测一个大于(>)设置数的数字来到达这里。

print('Done')
# 这最后一句语句将在
# if 语句执行完毕后执行。
```

输出:

```text
{% include "./programs/if.txt" %}
$ python if.py
Enter an integer : 50
No, it is a little lower than that
Done
$ python if.py
Enter an integer : 22
No, it is a little higher than that
Done
$ python if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
```

**它是如何工作的**
Expand Down Expand Up @@ -57,14 +89,40 @@ if True:

案例(保存为 `while.py`):

```text
{% include "./programs/while.py" %}
```python
number = 23
running = True

while running:
guess = int(input('Enter an integer : '))

if guess == number:
print('Congratulations, you guessed it.')
# 这将导致 while 循环中止
running = False
elif guess < number:
print('No, it is a little higher than that.')
else:
print('No, it is a little lower than that.')
else:
print('The while loop is over.')
# 在这里你可以做你想做的任何事

print('Done')
```

输出:

```text
{% include "./programs/while.txt" %}
$ python while.py
Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done
```

**它是如何工作的**
Expand All @@ -87,14 +145,22 @@ if True:

案例(保存为 `for.py`):

```text
{% include "./programs/for.py" %}
```python
for i in range(1, 5):
print(i)
else:
print('The for loop is over')
```

输出:

```text
{% include "./programs/for.txt" %}
$ python for.py
1
2
3
4
The for loop is over
```

**它是如何工作的**
Expand Down Expand Up @@ -125,14 +191,29 @@ if True:

案例(保存为 `break.py`):

```text
{% include "./programs/break.py" %}
```python
while True:
s = input('Enter something : ')
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')
```

输出:

```text
{% include "./programs/break.txt" %}
$ python break.py
Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something : use Python!
Length of the string is 11
Enter something : quit
Done
```

**它是如何工作的**
Expand Down Expand Up @@ -160,14 +241,29 @@ if you wanna make your work also fun:

案例(保存为 `continue.py`):

```text
{% include "./programs/continue.py" %}
```python
while True:
s = input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
print('Too small')
continue
print('Input is of sufficient length')
# 自此处起继续进行其它任何处理
```

输出:

```text
{% include "./programs/continue.txt" %}
$ python continue.py
Enter something : a
Too small
Enter something : 12
Too small
Enter something : abc
Input is of sufficient length
Enter something : quit
```

**它是如何工作的**
Expand Down

0 comments on commit 873f26b

Please sign in to comment.