How Range Is Used For Looping?


#1



 Hint
What if you use a for loop that does the appending in the range (0,5)?
How to even Do that ?


#2

I'll make make a general syntax..

hint**

for i in range(0,5):
    #append something in your list

#3

range() is a function that returns an iterator. This is very much like iterating a list.

a = range(10)
for i in a:
    print i + 1

for i in range(5):
    board.append(['O'] * 5)

This will produce a list of lists:

[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

#4

Thank You Guys :smiley:


#5

For good measure here is an explanation that covers both Python 2 and Python 3 versions of the range function...

http://pythoncentral.io/pythons-range-function-explained/


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.