练习33——while循环

来源:互联网 发布:murmurhash算法原理 编辑:程序博客网 时间:2024/04/29 06:40
# coding:utf-8# ex33 While 循环#while循环使用布尔值判断来决定是不是继续执行循环。可以实现比for更复杂的操作。#++++原练习代码++++i = 0numbers = []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" % iprint "The numbers: "for num in numbers:    print num#+++++++++++++++#++++根据加分题改写后的代码++++i = 0numbers = []maxium = int(input("please input a number!\n"))step = int(input("please input a step!\n"))while i < maxium:    print ("At the top i is %d" % i)    numbers.append(i)    i += step    print ("Numbers now:", numbers)    print ("At the bottom i is %d" % i)print ("The numbers:")for num in numbers:    print (num)#++++++++++++++++++++++++++#加分题:# 1.将这个 while 循环改成一个函数,将测试条件(i < 6)中的 6 换成一个变量。# 定义一个变量,我这里用用户输入的数字作为变量的值。↓# maxium = int(input("please input a number!\n"))# 把6替换成变量↓# while i < maxium:# 2.使用这个函数重写你的脚本,并用不同的数字进行测试。# 3.为函数添加另外一个参数,这个参数用来定义第 8 行的加值 + 1 ,这样你就可以让它任意加值了。# 同样的方法我添加了一个step变量。# 4.再使用该函数重写一遍这个脚本。看看效果如何。# 5.接下来使用 for-loop 和 range 把这个脚本再写一遍。你还需要中间的加值操作吗?如果你不去掉它,会有什么样的结果?# for循环中可以使用in range(a,b,c) a->起始值,b->结束值,c->跨度(step)值,从而实现和本脚本一样的效果
0 0
原创粉丝点击