0

I'm working through a Hangman problem in Python, which asks that I define a function that starts off with a string string.ascii_lowercase (the letters of the alphabet) and a given list lettersGuessed, and returns a string (in alphabetical order) of all letters that are not in lettersGuessed.

Here's what I've done so far:

def getAvailableLetters(lettersGuessed):
    s = string.ascii_lowercase[:]
    for letter in string.ascii_lowercase:
        if letter in lettersGuessed:
            s.replace(letter, '')
    return s

However, for every test value of lettersGuessed, this function just returns string.ascii_lowercase, and not s. Where am I going wrong?|

alexqwx
  • 147
  • 1
  • 7

2 Answers2

0

the method replace() does not edit the string. Instead, it returns a copy of the string in which the occurrences of old have been replaced with new. so the fifth line should look like this:

s = s.replace(letter, '')
Colin Nichols
  • 714
  • 4
  • 12
  • 1
    You mean s = s.replace(letter, '') – RvdK Mar 08 '17 at 22:49
  • I don't understand how an answer that repeats the incorrect code in the question (a common duplicate question, at that) gets an upvote... – TigerhawkT3 Mar 08 '17 at 22:53
  • lol Yes fixed. Thank you – Colin Nichols Mar 08 '17 at 22:54
  • 1
    @TigerhawkT3 How could I possibly have known that this was a duplicate question, and even if I did, how would I go about finding the original? – alexqwx Mar 08 '17 at 22:54
  • 1) It's a basic issue that's asked every day. 2) Google. – TigerhawkT3 Mar 08 '17 at 22:56
  • @TigerhawkT3 Yes, but as a new user, how am I supposed to know this? It's all well and good saying "just Google it", but when you're unsure of where the problem originates, it's not helpful. I wasn't looking for a solution to the problem; I was looking at why my solution didn't work. – alexqwx Mar 08 '17 at 22:58
  • 1
    @alexqwx - Test each part of your code individually to make sure it does what you think it does, look up the documentation for the functions you're using, Google for "function xyz doesn't work"... the usual debugging process. Posting a new question should be your last resort. – TigerhawkT3 Mar 08 '17 at 23:01
0

You should do as follows.

s = s.replace(letter, '')

instead of only:

s.replace(letter, '')

because, replace() returns returns a copy of the string with all occurrences of substring old replaced by new. Look into the following example.

s = "welcome to stackoverflow"
s.replace('stackoverflow', '')
print(s) # prints 'welcome to stackoverflow'
s= s.replace('stackoverflow', '')
print(s) # prints 'welcome to'
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • Better yet: `s = set(string.ascii_lowercase) - set(lettersGuessed); return "".join(c for c in s)` – Ricardo Branco Mar 08 '17 at 22:58
  • Ah. I thought that the '.' replaced the original string as it does for lists. How do you know, in general, whether the dot will replace a given (list, string, dictionary, etc.), or just print out the changed version? – alexqwx Mar 08 '17 at 23:00
  • @alexqwx mutable types like `list`, `dict`, and `set` will usually, well, mutate the original object. Immutable types (e.g `str`, `tuple`, `frozenset`) have to return a new one. – juanpa.arrivillaga Mar 08 '17 at 23:02
  • @alexqwx - You read the documentation for "the dot" (by which you mean an object's methods). – TigerhawkT3 Mar 08 '17 at 23:02
  • your best bet is to look it up in the Python documentation. You'll begin to remember over time. I usually just Google 'Python string replace' or whatever and the first link or two will tell you – Colin Nichols Mar 08 '17 at 23:03
  • @juanpa.arrivillaga Is that always the case, then, regardless of the actual "object method" used? – alexqwx Mar 08 '17 at 23:07
  • @alexqwx It is certainly almost always the case, there may be exceptions, I can't think of any off the top of my head, but methods mutate mutable objects, and return new objects for immutable objects. – juanpa.arrivillaga Mar 08 '17 at 23:13
  • @juanpa.arrivillaga Nice. Thanks – alexqwx Mar 08 '17 at 23:14