U
U
userpyy2020-05-26 14:01:05
Python
userpyy, 2020-05-26 14:01:05

Storing a variable in a for loop and using it outside?

Here is a piece of code, everything works except for the output of x and y

for (x,y,w,h) in heads1:
        x = w / 2 + x + sw
        y = h / 2 + y + sh
    print(x,y)

But if I try to write like this:
for (x,y,w,h) in heads1:
        x = w / 2 + x + sw
        y = h / 2 + y + sh
        print(x,y)

Everything is displayed well, but I need the variables to be displayed outside of this cycle

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin, 2020-05-26
@userpyy

x = y = None

for (x,y,w,h) in heads1:
    x = w / 2 + x + sw
    y = h / 2 + y + sh

print(x,y)

U
userpyy, 2020-05-26
@userpyy

Even after that, I don’t need to change it, roughly speaking, stupidly output it to the console, and that’s it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question