0

I have this kind of code. The problem is that 'kilpailija' is not changing for the other loop. Inside the third loop 'kilpailija' is always the first variable in the list numerot. How i can get it to change to that loop also?

k = 0
for kilpailija in numerot:
 while k < len(tulokset):
 for kilpailijanyt in tulokset[k]:
 if kilpailija == kilpailijanyt.hae_numero():
 tulos.append(kilpailijanyt.hae_tulos())
 if kilpailija != kilpailijanyt.hae_numero():
 tulos.append("-") 
 k += 1
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
asked May 8, 2014 at 14:29
3
  • 1
    Instead of have two if statements, why not an if else statement? Commented May 8, 2014 at 14:31
  • @JoachimPileborg: perhaps the kilpailijanyt.hae_tulos() call alters the state of the kilpailijanyt object? Of course, if it doesn't and kilpailijanyt.hae_numero() returns a consistent value, then an else would be great there. Commented May 8, 2014 at 14:33
  • Side note: PEP 8 recommends that English be used in the code (including in variable names), not Finnish. :) This can make your code easier to understand when you have to show it to other people, which in turn can help you. :) Not that it matters so much here, but this is a good habit to take. Commented May 8, 2014 at 15:15

2 Answers 2

1

Why use the while loop at all? You aren't using k for anything other than iteration:

for kilpailija in numerot:
 # I was going to use tulos for t here, but you
 # already have a variable named tulos.
 for t in tulokset:
 for kilpailijanyt in t:
 if kilpailija == kilpailijanyt.hae_numero():
 tulos.append(kilpailijanyt.hae_tulos())
 if kilpailija != kilpailijanyt.hae_numero():
 tulos.append("-")
answered May 8, 2014 at 14:54
Sign up to request clarification or add additional context in comments.

Comments

0

You never reset k in the inner loop.

The first iteration of for kilpailija in numerot runs the while loop until k < len(tulokset). Because neither k nor len(tulokset) change for the next iteration, that test is always going to be false from there on out.

Your outer loop works just fine, it is your while statement that is then never running the inner loops. Either reset k back to 0 inside the outermost loop, or use a different condition:

for kilpailija in numerot:
 k = 0
 while k < len(tulokset):
 for kilpailijanyt in tulokset[k]:
 if kilpailija == kilpailijanyt.hae_numero():
 tulos.append(kilpailijanyt.hae_tulos())
 if kilpailija != kilpailijanyt.hae_numero():
 tulos.append("-") 
 k += 1
answered May 8, 2014 at 14:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.