对于循环不打印Python 2中的列表中的所有内容

时间:2016-10-24 17:51:51

标签: python python-2.7

为什么这个for循环不能打印此列表/数组中的所有内容?例如,列表sentence的值为[0, 2],但只打印出0

# this variable is global, meaning it can be used everywhere.
numbers = []

def looping(number, increment):
    i = 0
    while i < number:
        print "At the top of i is %d" % i
        numbers.append(i)

        i = i + increment
        print "Numbers now: ", numbers
        print "At the bottom of i is %d" % i


looping(2, 2)

print "The numbers: "

for num in numbers:
    print num

1 个答案:

答案 0 :(得分:1)

您需要while i <= number:而不是while i < number: