+ 1

How to use input to replace elements in a list?

#TIC TAC TOE print('Enter number in an order : ') a=int(input()) b=int(input()) c=int(input()) d=int(input()) e=int(input()) f=int(input()) g=int(input()) h=int(input()) i=int(input()) list1=[a,b,c] list2=[d,e,f] list3=[g,h,i] if ((a==b==c)or(d==f==e)or(g==h==i)or(a==d==g)or(b==e==h)or(c==f==i)or(a==e==i)or(c==g==e))==1: print('player 1 won') elif ((a==b==c)or(d==f==e)or(g==h==i)or(a==d==g)or(b==e==h)or(c==f==i)or(a==e==i)or(c==g==e))==2: print('player 2 won') print(list1) print(list2) print(list3) I want to change the code such that, instead of entering the values in the list in an order , i want it to be like , when i enter d=2 in terminal input the list should be changed like this : [a,b,c] [2,e,f] [g,h,i]

1st Jun 2020, 11:05 AM
Mons Joseph
Mons Joseph - avatar
1 Antwort
+ 6
Here is a code that will do what you have requested. It has no error handling, and it has to be adpted and implemented in your code. ( its not the shortest code, but it is a readable version) def show_lst(): for i in lst: print(i) lst = [['a','b','c'], ['d','e','f'], ['g','h','i']] show_lst() inp = input('enter a character and a number (e.g.: b=3): ') for ind, row in enumerate(lst): if inp[0] in row: lst[ind][row.index(inp[0])] = inp[-1] show_lst()
1st Jun 2020, 1:23 PM
Lothar
Lothar - avatar