Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

to change value from 0 to 1 in a list of zeroes

n and k are user inputs, L is a list of zeroes.
if k is even, change the value to 1 of even indexes.
if k is odd, change the value to 1 of odd indexes.

but the output is just ones

[‘1′,’1′,’1′,’1’]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

can someone please guide me?

n=int(input())
k=int(input())

L=[0]*n
   for i in range(len(L)):
      if(k%2==0):
         L[i]='1'

      elif(k%2!=0):
         L[i]='1'


print(L)

>Solution :

In your logic, whether the value is even or odd, you are doing the same process at both steps. You should consider whether the index is odd or even in your logic.

to do this, modify your if statements to incorporate the index:

 for i in range(len(L)):
  if(k % 2 == 0 and i % 2 == 0):
     L[i]= 1

  elif(k % 2 != 0 and i % 2 != 0):
     L[i]= 1

This checks to ensure that not only the value you’re concerned with is even or odd, but if their index value is as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading