Home > other >  The output shows wrong results
The output shows wrong results

Time:06-13

Pls help me with this. It counts wrong. I think it would probably be because it counts from 1 but not from 0. How should i edit the code?

My task is: If the Array Element is less than 10 assign zero values, and assign items over 20 to 1

a = [1,3,4,5,6,34,51,44,12,42,1,4,2,31,30]
b = []
for i in a:
    if i < 10:
        b.append(0)
    elif i > 20:
        b.append(1)
print(b)

My output:

[0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1]

But it gotta be like this:

[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]

CodePudding user response:

The issue is you are only checking for less than 10 or more than 20, and there is a 12 in your array, which doesn't fit either. There really isn't a way you can edit the code to make it give you the desired output without drastically changing the rules you had before. Are you sure that [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1] is the correct output?

CodePudding user response:

What in situation when number will be between 10 and 20? I think here is the issue

  • Related