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
3 Respostas
+ 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
+ 1
Rae Thanks for the answer but please check the updated code and question.. sorry for updating late
0
Rae Thank you so much! 🎊😘