Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
john larson
16,594 PointsHow off target am I trying to get .replace to work in this scenario
I know there's other ways to do this, I just keep thinking there's a way to make this work. I don't really want to know another method to do this, I'm wondering why this doesn't work.
import random
import os
fruit_list = [
"apple",
"banana",
"cherry",
"dragonfruit",
"eggplant",
"fig"
]
fruit = random.choice(fruit_list)
#get an index, replace the correct guess at that index
fruit_str = len(fruit)* "_"
print("fruit_str: ", fruit_str)
guess = input("Guess a letter: ")
if guess in fruit:
index = fruit.index(guess)
if fruit_str[index] == "_":
fruit_str[index].replace("_", guess)
1 Answer
Ryan S
27,276 PointsHi John,
I like where you are going with that logic. But as far as I'm aware, .replace() has some limitations in what you are trying to do.
First, when using .replace() it only creates a copy of the string so you'd need to reassign it back to fruit_str in order to make permanent changes.
But the issue with it is that by default it will replace all instances with the character you give it. So if you have a string of 4 underscores, it will replace all 4 underscores with your guess (assuming it is found in "fruit")
It does have an optional 3rd argument called "count" where you can specify how many occurrences of the same character you want to replace, so if you were to do the following:
>>> fruit_str = "____" # 4 underscores
>>> fruit_str.replace('_', 'c', 2)
'cc__'
But again the issue with this is that it will always start replacing at index 0 and I don't believe you can use the index in any way to replace one particular underscore. The biggest hurdle is that strings are immutable and don't support direct item reassignment like lists do.
john larson
16,594 PointsThanks Ryan, I appreciate you helping me through that. With the things you pointed out I was able to get it to do what I wanted. Probably a little awkward and not very practical, but a learning process nonetheless :D
Ryan S
27,276 PointsNo problem.
Glad to hear you got it working.
On a side note, I found a simple but interesting method on stack overflow that uses slices and concatenation to replace specific string characters without having to convert into lists.
I know you weren't looking for other solutions, but I thought I'd share this anyways in case anyone was interested. It's a quick substitute for what you were trying to get out of the .replace() method.
>>> text = 'abcdefg'
>>> text = text[:1] + 'Z' + text[2:]
>>> text
'aZcdefg'
john larson
16,594 Pointsjohn larson
16,594 PointsThanks Ryan, using slice is one of the other things I was going to try after this. Now I have the reference :D