我使用LPTHW已经学习了大约2周的Python,这将是我第一次发布一个问题,所以在这里。首先是有问题的代码:
i = 0
numbers = []
print "Enter the number of times to run the loop: "
stop_i = int(raw_input('> '))
print "Enter the amount to increment the count by: "
increment = int(raw_input('> '))
def number_count(s_index,incr):
global i
while i < s_index:
print "At the top i is %d" % i
numbers.append(i)
i += incr
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: \n",
number_count(stop_i,increment)
for num in numbers:
print num
如果没有声明'global i',代码将无法运行,但出于某种原因,我不需要声明'全局数字'。我希望有人可以提供一些有关为什么这一点的见解?为什么“我”不属于函数范围,但“数字”是?