Okay, I have yet another question that I could not find.I noticed that you can't set variables in for loops without having to do something (like in batch). I've tried googling it and doing a site search, I have gotten no results. So what would the code or command be to allow you to set variables in loops, if a variable IS set in a for loop, do you have to do something special to print it in a for loop, like in batch if it's set in a for loop you need to put !'s around it. Your help would be appreciated.

-A.P.

Edited 7 Years Ago by AutoPython: n/a

In python, there is no difference between a variable in a for loop and a variable outside a for loop. I suggest that you forget all your batch and follow a good python tutorial step by step !

Then I can't figure out why my variables aren't getting set in my for loop.
EDIT:
*sigh*
I'm am so noob, I was trying to print the variables out side of the function which had the for loop in it.

Edited 7 Years Ago by AutoPython: n/a

Give us your code and we will help you understand.
For instance:

def myloop():
    for x in range(5):
        y = 2*x
    return y

# this will give 8, since the last x in the loop was 4
print(myloop())

Edited 7 Years Ago by Ene Uran: n/a

Oh, but now I do. I found out about the global statement. If I wanted to use the variable out side of the module I would've just made it global, but seeing as that wasn't necessary I just used the variable in the module. But if you must know my code, here it is:

input ()
r_l()
print (rand)

^^^
BAD
-----------------

global rand
   input ()
r_l()
print (rand)

^^^
sort of good
------------------

print (rand)
    input ()
r_l()

^^^
BEST!
-----------------


Before I was trying to use the print (rand) statement after the r_l or outside the module, when I moved it inside the module it worked. Again, like I said, if I had to use it outside the module I would've used the global statement, but when I posted this thread I didn't know that. Either way it's all good right now.

This question has already been answered. Start a new discussion instead.