习题33 while循环

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

这个程序并不难,需要知道的就是第一块里面的 append(i) 是为了给 numbers 加数字用的

第二节的 for 更不用说了,就是输出 numbers 里面的数字而已

运行结果如下:



=====================================================================================

附加练习:

1-4 参考

i = 0
print "Input a for 'while i < a': "
a = int(raw_input("> "))
print "Input b for 'i = i + b': "
b = int(raw_input("> "))
numbers = []

while i < a:
    print "At the top i is %d" % i
    numbers.append(i)
    
    i = i + b 
    print "Numbers now: ", numbers
    print "At the bottom i is %d" % i


print "The numbers: "

for num in numbers:
    print num

运行结果



5.

i = 0
print "Input a for 'i in range(0,a)': "
a = int(raw_input("> "))
#print "Input b for 'i = i + b': "
#b = int(raw_input("> "))
numbers = []

for i in range(0,a):
    print "At the top i is %d" % i
    numbers.append(i)
    
 #   i = i + b 
    print "Numbers now: ", numbers
    print "At the bottom i is %d" % i


print "The numbers: "

for num in numbers:
    print num

运行结果:



现在试一试去掉注释中的 i = i + b

结果就不贴了,好像没什么区别


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值