python学习13——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)

输出结果:

将数字6i 的增加值改成一个需要手动输入的值:

i = 0
numbers = []
a = int(input("Please enter a number:"))
while i < a:
 print("At the top i is %d." %i)
 numbers.append(i)
 b = int(input("Please enter a number again:"))
 i = i + b
 print("Numbers now:", numbers)
 print("At the bottom i is %d." % i)
print("The numbers:")
for num in numbers:
 print (num)

将while 改为 for

numbers_2 = []
c = int(input("Please enter a number:"))
for i in range(c):
 print("At the top i is %d." %i)
 numbers_2.append(i)
 print("Numbers now:")
 print("At the bottom i is %d."% i)
print ("The numbers:")
for num in numbers_2:
 print(num)

输出结果:

将加值操作去掉:

numbers_2 = []
c = int(input("Please enter a number:"))
for i in range(c):
 print("At the top i is %d." %i)
 
 print("Numbers now:")
 print("At the bottom i is %d."% i)

输出结果:

原文地址:https://www.cnblogs.com/shannon-V/p/9556085.html