+ 1

Learning core python and on list section

Asked to solve a problem of replacing an item in a list with the letter x. I do not know why this is wrong. items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) for y in items: if y==num: items[y]="x" else: continue print(items)

4th May 2021, 3:22 AM
Joshua Cronin
Joshua Cronin - avatar
8 Respuestas
+ 2
Hi! you have greatly complicated your program. why do you need a cycle? the num value is entered once and then an updated list is output. once. make the program as simple as possible
4th May 2021, 3:40 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
Jan, none of your versions are viable yet. x replaces the wrong index in the list. make it +1.
4th May 2021, 4:00 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
items = [1, 2, 3, 4, 5, 6, 7, 8,9, 10] num = int(input()) for i in range(len(items)): if items[i]==num: items[i]="x" print(items)
4th May 2021, 8:03 AM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
+ 1
I saw it already.:) . y is value not index. Try with the function range: for y in range(0,len(items)): if items[y]==num: items[y]="x" else: continue print(items) #Len returns the size of the list and range a list. We can combine them.
4th May 2021, 3:36 AM
Daniel Briceño
Daniel Briceño - avatar
+ 1
Daniel, you have the same problem. the position in the list is replaced by x, not the one that is needed, but by a position less. make an adjustment of +1
4th May 2021, 4:10 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Ярослав Вернигора(Yaroslav Vernigora) +1 : for y in range(0,len(items)): if items[y+1 or y]==num: items[y+1 or y]="x" ¿? Not reaching the end of the list will give me an error.
4th May 2021, 4:14 AM
Daniel Briceño
Daniel Briceño - avatar
+ 1
Your Answers is : -- items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = (int(input()) - 1) for y in items: if y==num: items[y]="x" else: continue print(items)
4th May 2021, 6:32 AM
🔰 Arit Dey 🔰
🔰 Arit Dey 🔰 - avatar
+ 1
Use this code
4th May 2021, 6:32 AM
🔰 Arit Dey 🔰
🔰 Arit Dey 🔰 - avatar