0

I have a list that is however many cells a user inputs. So say the user inputs 4, the list will consist of 4 0's: list_1 = [0,0,0,0].

Now the user is also asked which of these zeros he wants to replace with a 1 (must pick 2) and enters: 1 2. I want list_1[1] and list_1[2] to change from 0 to 1.

I have tried a few different things, but none are giving me the expected out put which should be a list [0,1,1,0] (if i was using the above code)

Any help is appreciated.

2
  • Attach the code please. Commented Apr 14, 2017 at 5:33
  • 2
    Can you show us what you've tried so far? Commented Apr 14, 2017 at 5:35

2 Answers 2

1
num_zero = int(input("Please enter the number of 0s")) #note this is python3
list_1 = num_zero*[0] #creates a list with the inputted number of zeroes
indices = input("Enter list indices: i j") #get string with two numbers in it sep by space
indices = indices.split(" ")  # create array with number strings
for s in indices: 
    i = int(s) #turn each number string into an int
    list_1[i] = 1 #set the specified indices of the list of zeroes to 1
Sign up to request clarification or add additional context in comments.

3 Comments

why so many downvotes? Marion and I gave OP what they asked for
I have no idea why people act like this? just click on downvote and go no reasons mentioned even to correct if its wrong.
It's especially annoying when both of our code works perfectly... and answers the OP's question
0
num_zero = input('Enter number of zeros you want in a list:')
zero_list = [0 for i in range(num_zero)]
indices = raw_input('Enter the indices you want to convert separated by space:')
index_list = map(int, indices.split())
for i in index_list:
    zero_list[i] = 1

1 Comment

Please explain your answer instead of just dumping a code snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.