Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

tcc8m

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

  • one year ago
  • one year ago

  • This Question is Closed
  1. msmithhnova
    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.

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

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

    • one year ago
  3. bwCA
    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.

    • one year ago
  4. bwCA
    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

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

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

    • one year ago
    • Attachments:

See more questions >>>

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.