+ 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?
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)
+ 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".
👍😉
+ 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")
+ 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
+ 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
+ 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
+ 2
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num = int(input())
# your code goes here
items[num] = "x"
print(items)
+ 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’
+ 1
This might help
https://code.sololearn.com/cN3wPX5bVoF4/?ref=app
+ 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 )
+ 1
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num = int(input())
items[num] = "x"
print(items)
+ 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)
+ 1
Stella Dee, your code worked... but I had to add break at the end to work for all. Thanks!!!
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)
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
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])
0
Ihor Princ you made it too long bro, you should have just replaced it
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")