Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

tcc8m

  • 2 years ago

Question on PS2, Hangman. Why do I need a break on the line after "string = string[0:a] + string[a+1:len(string)]"? "string = string[0:a]...." is towards the bottom of the code under the while loop. My code is in an attached link. If I don't put a break in, the error message is: IndexError: string index out of range http://pastebin.com/5WWnfmzE

  • This Question is Closed
  1. msmithhnova
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I place this line of code in before your if attempt == string[a]: ..... print 'a is', a ,'and attempt is', attempt, 'and string[a] is', string[a] You will notice 2 odd things, the letter after the one you chose is skipped and the last one is blank. This is because you are removing a letter from the string in your code causing the string to get shorter than what it was when the for loop initiated and therefore the for loop is now going one past the end of the string where it is one shorter. The break allows you to break out of the for loop after you have found the right character, eliminating the problem and also giving the program less to do since it doesn't have to search the rest of the letters even though it already found it's answer. Hope this helps.

  2. tcc8m
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    That was a great way to illustrate my problem! Thanks @msmithhnova .

  3. bwCA
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yep, it is a bad idea to modify a variable that you are using as a parameter of a for statement.

  4. bwCA
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    you may be able to rework your code using the 'in' operator http://pastie.org/4894198 http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#use-in-where-possible-1 http://docs.python.org/reference/expressions.html#not-in

  5. msmithhnova
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Yes, bwCA, that is how i coded mine, using 'in'.

  6. Not the answer you are looking for?
    Search for more explanations.

    Search OpenStudy
    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.