执行语句可以是单个语句或语句块

by admin on 2019年9月20日

The worlds can be one together
Cosmos without hatred
Stars like diamonds in your eyes
The ground can be space space space space space
With feet marching towards a peaceful sky
All the Moonmen want things their way
But we make sure they see the sun
Good bye Moonmen
You said good bye Moonmen
Good bye Moonmen
Good bye Moonmen

Python While 循环语句

By a lake,  two hamsters lived there, mother and son. One day, the
little hamster said to his mother, ‘Mom, I’d like to travel around the
lake a week.’

—— a song about genocide of all carbon matter

Python 编程中 while
语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:while
判断条件:

‘great!’ The mother hamster said, ‘well, have a nice trip.’

执行语句……

‘See you, mum.’ The little hamster went off on a trip.

执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空的值均为true。

After walking  for a while he saw an old turtle.

当判断条件假false时,循环结束。

‘Grandpa tortoise, where are you going?’ Asked the little hamster.

执行流程图如下:

‘Oh, little hamster, I’m going to the river below the hill to see my
grandson.’ The old turtle said kindly, and he paused, then said, ‘but I
am too slow, I am afraid I could not arrive on time.’

澳门美高梅手机版 1

The little hamster had a brainwave and said, ‘you can retract your head
and feet inside your shell. I’ll push you, and you’ll be there soon.’

Gif 演示 Python while 语句执行过程

‘That would be great’ the old turtle said slowly. ‘Thank you, the young
man!’

澳门美高梅手机版 2

So the little hamster pushed it and the old turtle rolled down. ‘Good
bye.’

复杂一点:

‘Good bye.’ As soon as they waved goodbye, hamster suddenly slipped and
fell into the water.

澳门美高梅手机版 3

‘Help澳门美高梅手机版,!……’ The little hamster only saw a shadow and was carried to
its back, then he passed out……

实例

It was second days when he woke up, he suddenly found the goldfish
beside the lake. ‘did you save me?’ Said the little hamster.

#!/usr/bin/python

“Yes,” answered the goldfish, ‘how did you fall into the lake?’

count = 0

‘I slipped, and I plan to travel around the lake for a week,” said the
little hamster.

while (count < 9):

The goldfish said, ’Really? I can carry you. Jump on my back.’

print ‘The count is:’, count

‘It’s amazing! thanks!’ said the little hamster.

count = count + 1

In this way, the little hamster continued its journey.

print “Good bye!”

澳门美高梅手机版 4

运行实例 »

以上代码执行输出结果:The count is: 0

The count is: 1

The count is: 2

The count is: 3

The count is: 4

The count is: 5

The count is: 6

The count is: 7

The count is: 8

Good bye!

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图