Why use the range function to iterate a for loop for a specific count?

This is a new piece of information

Based on your explanation patrickd314, I assigned a different integer and it does print out the statement 5 times instead of integer 1->5

promise = “I will not chew gum in class”
for i in range(5):
print(promise)

Just my 2 cents on the Lesson discussed above, but I think Codecademy was trying to let you see that you didn’t have to use the same “variable” assigment listed above the “for” loop as the same “temporary” variable assignment inside the “for” loop. Doing this causes the variable assigned i.e. “promise” to be overwritten in your script. You do not have to define the “temporary” variable to the one listed in the lesson either. You can assign it to anything you want. The “temporary” variable is just a “placeholder” to “temporary” store each iterator in the range function as it iterates one by one through the “for” loop. Attached is a screen capture of what my script answer was vs. Codecademy. I got the answer right, even tells you what lines do not “exactly” match theirs and even pinpoints the exact location where your script differs from theirs. Here, the difference was in line 3 and the character “n” not being their “i” but the concept stands valid.

1 Like

in the exercise we were asked to create a for loop to print the statement inside the “promise” variable 5 times … in a range of (0,5)
so I was pretty lazy today and I figured since the statement is already assigned to a variable , I won’t use it as temporary variable … and this is how my code went :

promise = "I will finish the python loops module!" for word in range(5): print(promise)

I used another temporary variable to iterate in the loop so I can be able to print the variable "promise without having to type in the whole statement
… And baam it worked … my question is : is it ok to do this anytime i’m feeling lazy like i did today or is it risky ? cause for me since it worked then it’s cool .

1 Like

Great explanation! Always good to dive deep into concepts! Thank you for that!

1 Like

This is the meaning of for-loops in my opinion and honestly I think the exercise was meant like this! you declare a variable (promise) and than you create a for loop to do some action using this variable. Why would you write it again in the loop? Not sure if this is laziness, I think it’s meant to be so…

1 Like

I bleive you can use promise as well since it’s already defined.

For example, when we are given

promise = “I will not chew gum in class”

instead of doing:

for promise in range(5):
print(“I will not chew gum in class”)

the other option that worked for me in the exercise you’re referring to, I used:

for i in range(5):
print(promise)

they both give you the output you’re looking for, but since the string “I will not chew gum in class” is assigned to promise, it would be more efficient to use the defined variable.

for promise in range(5): promise =“I will not chew gum in class” print(promise)

you have to declare the var promise before the for loop to be able to recall it back again inside the for loop

that can make it annoying, and hard, in cases where you’re used to the style not in python