tklein
tklein

Reputation: 77

Iterating a loop a limited number of times

I know questions like this have been asked countless times, but nothing I've found seems to work in this case. How do I make this code run once for every i? It's an infinite loop printing the same list over and over right now.

If I remove the while loop or nest the append method inside the for loop, it doesn't work as intended. I need it to multiply by 2 (2, 4, 8, 16, 32, etc) 64 times.

board = []
def count():
    for i in range(1,65):
        while i:
            i = (i)*2
            board.append(i)
            if i in board is True:
                break
            print(board)
count()

Upvotes: 0

Views: 9801

Answers (4)

AGN Gazer
AGN Gazer

Reputation: 8378

Below is the simplest modification of your code. What you want to do actually can be achieved much simpler in Python (such as board = [2**i for i in range(1,65)]). I just want to illustrate what needs to be removed from your code to make it do what you want it to do (as I understood it):

board = []
def count():
    j = 1
    for i in range(1, 65):
        j *= 2 # we do not want to modify loop's variable
        board.append(j)
    print(board)
count()
  1. You do not need while i. It turns out it is an infinite loop because if i in board is True is always false (see Iterating a loop a limited number of times). So you are running an infinite loop. I think you actually intended that while-loop break after the first iteration and then you should have modified the condition to be if i in board or if (i in board) is True both being ALWAYS true => no need for loop.
  2. You do not need to check that i is in the board since in the previous statement you are appending it to the board so that you know for sure that i is in the board.
  3. You have i = (i)*2. It is not advisable to modify loop's variable (so I introduced j). For example if I kept your code i = i * 2 then i would have been reset to the next value of the loop variable at the next iteration thus forgetting that i was set to the double its value.
  4. Move print(board) outside the loop or you will get a very long output.

Upvotes: 2

Lucas Veiga Fujicava
Lucas Veiga Fujicava

Reputation: 104

Using your code as a starting point, it can be modified the following:

board = []
def count():
    total = 1
    for i in range(1,65):
        total = total*2
        board.append(total)
        print(total) # just so you can better visualize the output
count()

It outputs:

2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
4294967296
8589934592
17179869184
34359738368
68719476736
137438953472
274877906944
549755813888
1099511627776
2199023255552
4398046511104
8796093022208
17592186044416
35184372088832
70368744177664
140737488355328
281474976710656
562949953421312
1125899906842624
2251799813685248
4503599627370496
9007199254740992
18014398509481984
36028797018963968
72057594037927936
144115188075855872
288230376151711744
576460752303423488
1152921504606846976
2305843009213693952
4611686018427387904
9223372036854775808
18446744073709551616

However, a simpler and more elegant way to do it would be board = [2**i for i in range(1,65)] as noted by AGN.

Upvotes: 1

Ketan Mukadam
Ketan Mukadam

Reputation: 859

How about this code which will give you power of 2s till 2**64. I assume that is what is required

board = [2**i for i in list(range(1,65))]
print(board)

Upvotes: 2

Rachit
Rachit

Reputation: 248

Your loop is running infinitely because while i is always true. Try removing while loop and if condition as well. Also, introduce a new variable to double its value as i becomes 64 in 7 times but you need 64 values.

Upvotes: 1

Related Questions