Jump to content

Why does my array lose False/empty variables?

Featured Replies

Hey everyone, hope you are doing fine in these tumultuous times!

Edit: I found my mistake: I used 'if' instead of 'elif', therefore the code executes: is r larger than 2, if not, ArrayB = Array (which is the original array of length 10).

I have the following code, what I don't understand is that without the 'if r > 2:' part of the code, len(ArrayB) will be 20 in both cases (r =2) but with this piece of code, which is not running, len(ArrayB) becomes 10 (= losing False variables). I don't understand how why this piece of code affects the rest of my code, when r is not larger than 2?

I am running python 3.8 in Pycharm

r = 2
Array = [0,1,2,3,4,5,6,7,8,9]

if r < 1:
    ArrayB = [1] * int(r*len(Array))
    Const = len(Array)/len(ArrayB)
    for i in range(len(ArrayB)):
        ArrayB[i] = Array[int(i*Const)]

if r > 1 and r <= 2 :
    ArrayB = [False]*int(r*len(Array))
    for i in range(len(Array)):
        ArrayB[int(i*r)] = Array[i]
    print(len(ArrayB))					# First len(ArrayB)
    print(ArrayB)					# First print(ArrayB)

# if r > 2:  						#Uncomment this part and len(ArrayB) afterwards becomes 10
#     print('r is bigger than 2')
#     ArrayB = [False] * int(r * len(Array))
#     for i in range(len(Array)):
#         ArrayB[int(i*r)] = Array[i]

else:
    ArrayB = Array
print(len(ArrayB))					# Second len(ArrayB)
print(ArrayB)						# Second print(ArrayB)

output with all code active:
20
[0, False, 1, False, 2, False, 3, False, 4, False, 5, False, 6, False, 7, False, 8, False, 9, False]
10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

output without if r >2: code:

20
[0, False, 1, False, 2, False, 3, False, 4, False, 5, False, 6, False, 7, False, 8, False, 9, False]
20
[0, False, 1, False, 2, False, 3, False, 4, False, 5, False, 6, False, 7, False, 8, False, 9, False

Thank you for your time! Edit: problem found, if statements should be elif

- Dagl

Edited by Dagl1

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.