1

I'm new at python and recently I've been trying to write a code for a hangman game: a part of the MIT OCW course: Introduction to Computer Science and Programming. My code goes as follows:

def write_words (word, al):
    neww = (list(word))
    newal = (list(al))
    x = 0
    while (x <= (len(newal))):
        z = newal[x]
        y = neww.index(z)
        except ValueError:
            pass
        x = x + 1
        return (z)
        return (y)

when I call the function using write_words ("word", "abcdefghijklmnopqrstuvwxy") I still get the ValueError:'a' is not in list, that is supposed to be corrected with the exception. I 've been trying to figure out the problem, and apparently is a Syntax error. Please, I would be very grateful with any help. My python version is the 3.2.1

1
  • You seem to have your return statement inside the while loop with no conditions. That would return from the function in the first loop. Can you please explain what you are trying to do here? Commented Aug 2, 2013 at 3:55

1 Answer 1

4

You don't have a try statement there. The format is try-except. Something like this.

try:
     a = 25 / 0
except ZeroDivisionError:
    print("Not Possible")


# Output: Not Possible
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.