复习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(65)
。 转载地址:https://memcpy0.blog.csdn.net/article/details/79935832 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月30日 06时30分59秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
剑指offer:数组中出现次数超过一半的数字(java)
2019-05-03
剑指offer:最小的k个数(java)
2019-05-03
剑指offer:连续子数组的最大和(java)
2019-05-03
剑指offer:从1到n整数中1出现的次数(java)
2019-05-03
剑指offer:把数组排成最小的数(java)
2019-05-03
剑指offer:丑数(java)
2019-05-03
剑指offer:第一个只出现一次的字
2019-05-03
剑指offer:数组中的逆序对(java)
2019-05-03
剑指offer:两个链表的第一个公共结点(java)
2019-05-03
剑指offer:数字在排序数组中出现的次数(java)
2019-05-03
实时开发框架Meteor API解读系列<二>Core
2019-05-03
实时开发框架Meteor 实际应用系列<一>---文件的上传和下载
2019-05-03
实时开发框架Meteor API解读系列<四>Server connections
2019-05-03
实时开发框架Meteor API解读系列<六> DDP
2019-05-03
实时开发框架Meteor 实际应用系列<一>---文件的上传和下载[补充]
2019-05-03
实时开发框架Meteor API解读系列<七> Collection --01
2019-05-03
启用fcitx-qimpanel面板程序
2019-05-03
浅谈Q的基本实现
2019-05-03