+ 2

# List Operations V

You are given a list of items. Write a program that takes a num number as input, reassigns the element with that index in the list to the value "x" and outputs the updated list. For example, for a given list [1, 2, 3, 4, 5] and input 3, the output should be: [1, 2, 3, "x", 5] HERES My CODE: items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) # your code goes here if num in items: items[num] = 'x' print(items) else: print(items) There’s one hidden case test wrong and I can’t see what’s wrong with it?

26th Nov 2020, 8:28 PM
Andrei Gherca
18 odpowiedzi
+ 9
try this items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) # your code goes here items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] items[num] = "x" print(items)
4th Jan 2021, 10:14 AM
Elio
+ 5
You have to replace the item at the index of the input number. So you do: index = items.index(num) To get the index of the input number. And: items[index] = "x" To replace it with an "x". 👍😉
26th Nov 2020, 9:18 PM
🔥EACY🔥
+ 5
#try this items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) if num in items: items[num-1] = 'x' print(items) else: print("Out of range")
27th Nov 2020, 2:04 AM
Simba
+ 3
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) if num == 0: items[0] = 'x' print( items ) elif num == 1: items[1] = 'x' print( items ) elif num == 2: items[2] = 'x' print( items ) elif num == 3: items[3] = 'x' print( items ) elif num == 4: items[4] = 'x' print( items ) elif num == 5: items[5] = 'x' print( items ) elif num == 6: items[6] = 'x' print( items ) elif num == 7: items[7] = 'x' print( items ) elif num == 8: items[8] = 'x' print( items ) elif num == 9: items[9] = 'x' print( items ) elif num == 10: items[10] = 'x' print( items ) elif num == 11: items[11] = 'x' print( items ) Long😂💔 but works with no bugs
27th Dec 2021, 1:39 PM
Emmanuel Francis
+ 2
List index starts from 0. so for 0, list can't change. If input is 10 then list[10] will raise error. Edit : Andrei Gherca I think you need to exclude10 input
26th Nov 2020, 8:35 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 is actually right. The code also goes wrong in unsorted lists. What you are about to do is to replace an element in a list, where the content of the list is given. Not the index is given. So the code where you access the array for replacement should look something like items[items.index(num)] = 'x' Hope that helps. Cheers. C
26th Nov 2020, 8:48 PM
ChrA
+ 2
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) # your code goes here items[num] = "x" print(items)
29th Sep 2021, 6:19 AM
Bernard Towindo
+ 1
ChrA , i tryed your edit and now all cases are coming up as wrong. Have a look at the example they gave in the 2nd paragraph: For example, for a given list [1, 2, 3, 4, 5] and input 3, the output should be: [1, 2, 3, "x", 5] in your edit, input 3 would change the [3,] to an ‘x’ when its supposed to change the [4,] to an ‘x’
26th Nov 2020, 9:07 PM
Andrei Gherca
26th Nov 2020, 9:25 PM
Tomiwa Joseph
+ 1
items = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] num = int( input() ) if num in range( len( items ) ) items[ num ] = 'x' else: print( "Invalid index" ) print( items )
27th Nov 2020, 3:24 AM
Ipang
+ 1
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) items[num] = "x" print(items)
23rd Mar 2021, 11:42 PM
Ankit Dogra
+ 1
Try items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) # your code goes here for i in range(len(items)): items[num] = 'x' print(items)
14th Apr 2021, 3:26 PM
Stella Dee
+ 1
Stella Dee, your code worked... but I had to add break at the end to work for all. Thanks!!!
9th May 2021, 5:15 AM
Julio Moreno
0
😳 Sorry, I was anticipating a different task from the settings I saw. Well, it might boil down to the answer of Jayakrishna🇮🇳 that you need to catch indexes largen than the lists length (-1)
26th Nov 2020, 9:22 PM
ChrA
0
It's funny you guys like to consider the "ERROR" input from users.Indeed,no need to worry about that,🐍PYTHON🐍will handle it
28th Nov 2020, 4:13 PM
yingSSS['book'=="本"]
0
my solution is items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) if num == 1: print([1, "x", 3, 4, 5, 6, 7, 8, 9, 10]) elif num == 2: print([1, 2,"x", 4, 5, 6, 7, 8, 9, 10]) elif num == 3: print([1, 2, 3,"x", 5, 6, 7, 8, 9, 10]) elif num == 4: print([1, 2, 3, 4, "x", 6, 7, 8, 9, 10]) elif num == 5: print([1, 2, 3, 4, 5, "x", 7, 8, 9, 10]) elif num == 6: print([1, 2, 3, 4, 5, 6, "x", 8, 9, 10]) elif num == 7: print([1, 2, 3, 4, 5, 6, 7, "x", 9, 10]) elif num == 8: print([1, 2, 3, 4, 5, 6, 7, 8,"x", 10]) elif num == 9: print([1, 2, 3, 4, 5, 6, 7, 8, 9, "x"]) else: print(["x", 2, 3, 4, 5, 6, 7, 8, 9, 10])
8th Jan 2022, 8:02 PM
Ihor Princ
0
Ihor Princ you made it too long bro, you should have just replaced it
9th Jan 2022, 6:22 AM
Emmanuel Francis
0
There is some problem while unlocking of hidden case pls provide a better solution a=input() #your code goes here commands=["Lights Off","Lock the door", "Open the door","Make Coffee","Shut down"] if(a in commands): print("OK") elif (a not in commands): print("Not Supported")
6th Nov 2022, 4:32 PM
Harivijay Chormage