朝文分享(14):Python入门指南(四)-while语句篇

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard新学苑。

今天小编为大家带来“Python入门指南”,欢迎您的访问。

Share interest, spread happiness, increase knowledge, and leave beautiful.

Dear, this is the LearingYard Academy!

Today, the editor brings the Getting Started with Python,welcome to visit!



一、while语句

While语句是循环语句的一种。先通过条件判断,如果满足条件,就执行内部的指令,直到不满足该条件为止。如果本就不满足条件,那么不执行内部指令。下面是一段猜数字的代码。获取随机数需要导入random,用random.randint语句获取区间内的随机数。循环内用到上次学习的if语句对所猜数字比答案大还是小。每循环一次,i值增加1一次,用于记录猜的次数。

The While statement is a type of loop statement. If the condition is met, the internal instruction is executed until the condition is not met. If the condition is not met, the internal instruction is not executed. Here is a piece of code to guess the number. To obtain random numbers, you need to import random, and use random.randint statement to obtain random numbers within the interval. The loop uses the if statement learned last time to determine whether the number guessed is greater or smaller than the answer. For each cycle, the value of i is increased by 1, which is used to record the number of guesses.

二、while语句的嵌套

While语句中包含另外的while语句,这就叫while语句的嵌套,用于解决多重循环问题。下面是一段送花表白的代码。如果用一层表白的循环,里面送花的代码就要写十条,很麻烦;如果只用送花的循环,外面表白的代码就要写一百次,更麻烦。所以,这里用到while语句的嵌套,轻松实现多重循环。

The While statement contains another while statement, which is called the nesting of while statements, to solve the problem of multiple loops. The following is a piece of flower confession code. If you use a layer of confession of the cycle, inside the flower code to write ten, very troublesome; If you only use the cycle of sending flowers, the code outside will be written a hundred times, which is more troublesome. Therefore, the nested while statement is used here to easily implement multiple loops.



今天的分享就到这里了,

如果您对文章有独特的想法,

欢迎给我们留言。

让我们相约明天,

祝您今天过得开心快乐!

That's all for today's sharing.

If you have a unique idea about the article,

please leave us a message,

and let us meet tomorrow.

I wish you a nice day!

参考资料

翻译:网易有道翻译

本文由LearningYard学苑整理并发出,如有侵权请后台留言沟通.

文案|Dongyang

排版|Dongyang

审核|hong

Learning Yard 新学苑

展开阅读全文

页面更新:2024-02-29

标签:语句   随机数   嵌套   网易   指令   入门   条件   留言   代码   数字   指南   快乐

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top