 Learning core python and on list section | Sololearn: Learn to code for FREE!

+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)

+5

# corrected items = [0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input('num: ')) for y in items: if y==num: items[items.index(num)]="x" else: continue print(items) # better with list comprehension items = [0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input('num: ')) items = [i if i!=num else 'x' for i in items] print(items)

+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

+2

Jan, none of your versions are viable yet. x replaces the wrong index in the list. make it +1.

+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)

+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.

+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

+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.

+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)