#๐ how to replace a string with a specific item in a list
65 messages ยท Page 1 of 1 (latest)
@rose aspen
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
!code
#Step 2
import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
#Testing code
print(f'Pssst, the solution is {chosen_word}.')
#TODO-1: - Create an empty List called display.
#For each letter in the chosen_word, add a "_" to 'display'.
#So if the chosen_word was "apple", display should be ["_", "_", "_", "_", "_"] with 5 "_" representing each letter to guess.
guess = input("Guess a letter: ").lower()
display = []
for i in range(0,len(chosen_word)):
display.append("_")
#TODO-2: - Loop through each position in the chosen_word;
#If the letter at that position matches 'guess' then reveal that letter in the display at that position.
#e.g. If the user guessed "p" and the chosen word was "apple", then display should be ["_", "p", "p", "_", "_"].
for letter in chosen_word:
for i in range(0,len(chosen_word)):
if letter == guess:
display[i] = guess
#TODO-3: - Print 'display' and you should see the guessed letter in the correct position and every other letter replace with "_".
#Hint - Don't worry about getting the user to guess the next letter. We'll tackle that in step 3.
print(display)
need help with TODO-2 plz
i want to replace the correct enrty in the display list with the letter of the chosen_word if the letter == guess
but i keep getting this
but i only want the 'a' to be in the 2nd entry of the list
how do i do this??
Okay, so why are you using 2 for loops for iterating over your word once? Letter will not be changed in your inner for lop and overwrite your entire list.
!d enumerate
enumerate(iterable, start=0)```
Return an enumerate object. *iterable* must be a sequence, an [iterator](https://docs.python.org/3/glossary.html#term-iterator), or some other object which supports iteration. The [`__next__()`](https://docs.python.org/3/library/stdtypes.html#iterator.__next__) method of the iterator returned by [`enumerate()`](https://docs.python.org/3/library/functions.html#enumerate) returns a tuple containing a count (from *start* which defaults to 0) and the values obtained from iterating over *iterable*.
```py
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
``` Equivalent to...
This might be useful for you here
well i kinda want to go through each entry of the 'chosen_word'
so 0 is the 1st entry
and so if its equal to the guess
then display[0] would be replaced with the letter
thats my aim
Yeah I understand what your code is supposed to do, but I'm saying you don't need to use 2 separate for loops when one would be enough.
ok
That is also the origin of your error, since once the letter == guess is True for the letter from your outer for loop, your inner for loop will overwrite your entire display list with guess
yh ok
so how can i do what i want to do
Well I could give you a working solution if you want, but for your learning I think it would be better if you figured it out yourself.
Try removing your inner for loop, and maybe look into enumerate it might make this easier for you
||py for i, letter in enumerate(chosen_word): if letter == guess: display[i] = guess||
Here's a solution, but try to figure it out yourself first ^^
yh id rather figure it out myself
is there another way instead of using enumerate
using simpler lingo
Yeah
could u give me a hint for that plz
Just iterate over your word extracting a letter using its index
ok
Then you can use the index variable to assign the respective position in display
would i need to use the range function?
Yes
ok
Looks good ๐
for letter in range(len(chosen_word)):```
This would be also valid, since 0 is implied by range if you don't specify a start value
do i need to convert my chosen_word into a list with all its characters?
But your's is also valid
No you can index a string directly, just like you would a list
!e
string = "test"
print(string[2])```
:white_check_mark: Your 3.12 eval job has completed with return code 0.
s
!code
#Step 2
import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
#Testing code
print(f'Pssst, the solution is {chosen_word}.')
#TODO-1: - Create an empty List called display.
#For each letter in the chosen_word, add a "_" to 'display'.
#So if the chosen_word was "apple", display should be ["_", "_", "_", "_", "_"] with 5 "_" representing each letter to guess.
guess = input("Guess a letter: ").lower()
display = []
for i in range(0,len(chosen_word)):
display.append("_")
#TODO-2: - Loop through each position in the chosen_word;
#If the letter at that position matches 'guess' then reveal that letter in the display at that position.
#e.g. If the user guessed "p" and the chosen word was "apple", then display should be ["_", "p", "p", "_", "_"].
for i in range(0,len(chosen_word)):
if chosen_word[i] == guess:
display[i] = guess
#TODO-3: - Print 'display' and you should see the guessed letter in the correct position and every other letter replace with "_".
#Hint - Don't worry about getting the user to guess the next letter. We'll tackle that in step 3.
print(display)
think ive got it
Yeah good job. Looks good to me.
perfect thanks a lot for ur help
appreciate it
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.