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)
输出结果:
将数字6 和 i 的增加值改成一个需要手动输入的值:
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)
输出结果: