0

[SOLVED] How to replace two same items in a list ?

I just want output as ["c", "_", "c", "_" ] see my attempt you will understand what i want.. word="cock" list=["_", "_","_","_" ] just want to replace the underscore which has same index as word "c" so that my new list will be ["c", "_", "c", "_" ] simple and less code will be good 😜 Thanks! You all know hangman game right? that is what I am trying to do... but I am sink into erros and bugs... lol my attempt : https://code.sololearn.com/c99iLxxZG8Ud/?ref=app

23rd Apr 2021, 6:49 PM
Ratnapal Shende
Ratnapal Shende - avatar
3 odpowiedzi
+ 5
Ratnapal Shende updated the code Input in sololearn: c k o word="cock" lst=['_']*len(word) print(lst) ans = lst while True : print() char=input("Enter a character : ") print() lst = [char if word[i]==char else ans[i] for i in range(len(lst))] print(lst) ans = lst if ''.join(ans) == word: print("\nYou won!") break [OR] word="cock" lst=['_']*len(word) print(lst) ans = lst while True : print() char=input("Enter a character : ") print() for i in range(len(lst)): if word[i]==char: lst[i]=char else: lst[i]=ans[i] #char if word[i]==char else ans[i] for i in range(len(lst))] print(lst) ans = lst if ''.join(ans) == word: print("\nYou won!") break
23rd Apr 2021, 7:25 PM
minirkk
minirkk - avatar
+ 1
Rae Thanks for the answer but please check the updated code and question.. sorry for updating late
23rd Apr 2021, 7:28 PM
Ratnapal Shende
Ratnapal Shende - avatar
0
Rae Thank you so much! 🎊😘
23rd Apr 2021, 8:48 PM
Ratnapal Shende
Ratnapal Shende - avatar