#๐Ÿ”’ how to replace a string with a specific item in a list

65 messages ยท Page 1 of 1 (latest)

rose aspen
#

need help with TODO - 2

stuck rockBOT
#

@rose aspen

Python help channel opened

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.

rose aspen
#

!code

stuck rockBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

rose aspen
#
#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??

mint garnet
#

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

stuck rockBOT
#

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...
mint garnet
#

This might be useful for you here

rose aspen
#

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

mint garnet
#

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.

rose aspen
#

ok

mint garnet
#

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

rose aspen
#

so how can i do what i want to do

mint garnet
#

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 ^^

rose aspen
rose aspen
#

using simpler lingo

mint garnet
#

Yeah

rose aspen
#

could u give me a hint for that plz

mint garnet
#

Just iterate over your word extracting a letter using its index

rose aspen
#

ok

mint garnet
#

Then you can use the index variable to assign the respective position in display

rose aspen
mint garnet
#

Yes

rose aspen
#

ok

rose aspen
#

like this?

mint garnet
#

Looks good ๐Ÿ‘

rose aspen
#

ok

#

think i got an idea

mint garnet
#
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
rose aspen
#

do i need to convert my chosen_word into a list with all its characters?

mint garnet
#

But your's is also valid

#

No you can index a string directly, just like you would a list

rose aspen
#

oh

#

i never knew

#

amazing

dull oasis
#

a string is kinda a type of an array

#

character array

mint garnet
#

!e

string = "test"
print(string[2])```
stuck rockBOT
rose aspen
#

!code

stuck rockBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

rose aspen
#
#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)

rose aspen
mint garnet
#

Yeah good job. Looks good to me.

rose aspen
#

appreciate it

#

!close

stuck rockBOT
#
Python help channel closed

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.