match vectors using variables

Asked

Viewed 37 times

-3

Guys I just started in python, I wonder what’s wrong with that code. I wanted to pass the numbers of valores for valoresI always skipping a position, so the x += 2. But he keeps making mistakes. If I put valoresI[4] = valores[4] he even goes, but I needed to use a variable that changed this value of 2 in 2

while x <= 10:

    valoresI[x] = valores[x]
    x += 2
  • 1

    What error? From what I see can be this "x" there, in which case the result of your list valoresl will be half of the list correct values? Then Voce can make x = 0, and then run a while loop that will iterate your list/2 and each iteration x will be added by 2

  • as you initialized x, valoresI and valores?

  • values = [] valueI = [] x = 0

1 answer

0


The error is happening because you need to initialize the variable. You’re trying to add 2 to it, but it doesn’t exist yet, so it has no value.

Remember that a variable nula is not the same as her being equal to 0.

If your vectors are ok, then simply initializing the variable should solve your problem:

x = 0;
while x <= 10:
    valoresI[x] = valores[x]
    x += 2

Note that a for would go well in this situation

for x in range(0, 10, 2):
    valoresI[x] = valores[x]
  • 1

    thank you very much, solved my problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.