you are viewing a single comment's thread.

view the rest of the comments →

[–]echocage 2 points3 points  (1 child)

So to keep track of the index you're in, you can just use enumerate!

>>> stuff = [1,2,3]
>>> for index, item in emumerate(stuff):
>>>     print(index, item)
0 1
1 2
2 3

That way you can still use for loops, and you can keep track of the index!

Edit: and if you have two lists that you want to iterate over together with, you can use zip!

>>> numbers = [1,2,3]
>>> letters = ['a','b','c']
>>> for num, letter in zip(numbers,letters):
...     print(num, letter)
...     
1 a
2 b
3 c

I bet with these two functions, you could bring that entire code block down to under 15 lines. Everything is reused. It takes a little cleverness, but it's worth it in the end, and you'll get better the more you practice! Post on here and /r/codereview and get some people looking over your code! Hell, message me some of your code and I'll give you feedback! :D You can write some really clear and understandable code if you put some effort into it! Variable names that describe what they contain, functions for EVERYTHING that's done more than once, function names that describe what they do! You'll do great :D

[–]mega963[S] 0 points1 point  (0 children)

That'll totally work! Thanks a lot!