复习Python的Day30
发布日期:2021-07-01 03:03:11 浏览次数:2 分类:技术文章

本文共 1203 字,大约阅读时间需要 4 分钟。

复习Python的第三十天

习题33:While 循环

本节介绍的是while-loop(while循环)。while-loop会一直执行它下面的代码片段,直到它对应的布尔表达式为False时才会停下来。

书中提到:

为了避免“死循环”,你需要遵循下面的规定:

1. 尽量少用while-loop,大部分时候 for-loop 是更好的选择。
2. 重复检查你的 while语句,确定你测试的布尔表达式最终会变成False 。
3. 如果不确定,就在 while-loop 的结尾打印出你要测试的值。看看它的变化。

i = 0 numbers = [] while i < 6:     print "At the top i is %d" % i     numbers.append(i)    i = i + 1     print "Numbers now: ", numbers     print "At the bottom i is %d" % i print "The numbers: " for num in numbers:     print num

加分习题:

几个题就一起写了:

# -*- coding : utf-8 -*-numbers = []def while_loop(var, begin=0, stride=1):    while begin < var:        print "At the top i is %d" % begin        numbers.append(begin)        begin += stride        print "Numbers now:", numbers        print "At the bottom i is %d" % begin    print "The numbers:"    for num in numbers:        print numvar = int(raw_input("Please input a number:"))# begin = int(raw_input("Please input the beginning number:"))stride = int(raw_input("Please input the stride number:"))while_loop(var, stride)

注意:raw_input返回的是字符串str,必须转换为int对象,不然会失误。即使你输入的是数字如6,Python得到的将是’6’的ASCII码值。

Python中,将ASCII字符转换为对应的ASCII码数值,如:‘a’–>65,使用ord函数,ord('a')

反之,使用chr函数,将数值转换为对应的ASCII字符,chr(65)

转载地址:https://memcpy0.blog.csdn.net/article/details/79935832 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:复习Python的Day32:复习各种符号
下一篇:复习Python的Day29 循环和列表

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月30日 06时30分59秒