I'm a novice and learning python (using 2.7). I'm attempting some simple scripts to test how python handles different types of loops.
My question is: can python change the starting point of the "range" function on each iteration if the start point is assigned to a variable? Here is an example of my code:
def build(n, period):
n2 = n
for digit in range(int(n2), 20):
print "At the top i is %d" % n
digit += period
numbers.append(digit)
print "Numbers now:", numbers
print "At the bottom i is %d" % digit
print "The Numbers:"
n2 += period
for num in numbers:
print num
key = raw_input("Press 1 to call function \"build\", press any other key to quit.")
if key == "1":
i = raw_input("What integer is our start value?")
amt = raw_input("What is the common difference?")
numbers = [int(i)]
build(int(i),int(amt))
else:
quit()
I tried to use a second local variable 'n2' inside the function so I could keep the initial value of 'n' constant and then redefine the range for each iteration. The very first number in the appended list moves by the common difference but after that it always steps by +1 integer. I can easily make this happen with a 'while' loop but curious if a 'for' loop can be used to accomplish this?
whileloop if you are looking for other loops -while n2 < 20: n2+= period. Also you will have to pass thenumbersarray to the function. Else you can call it as global variable.