A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Ending a simple loop This is really pre-first problem set. The sample exercise is : Write a program that has a user guess your name, but they only get 3 chances to do so until the program quits. I want to make a simple loop, but can't find a way to end the 'NOPE' branch without printing 'good guess' as well. Suggestions? Thanx. name = 'Monty' guess = "" i = 0 while guess != name : if i < 3: i = i + 1 guess = raw_input('Guess my name: ') else: print 'NOPE' print 'good guess'

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

    The simplest way to fix this code is to insert another test just before the last line: if guess == name: print 'good guess'

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

    Thanks-- what about how to keep it from printing NOPE forever, if three wrong answers are entered, since guess is still != name?

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

    I tried to stop the loop by telling it the top condition was met, but then I can't seem to get out of printing 'good guess.'

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

    I would change the loop to include a test of whether 1>3. If I was writing this for the first time, I think that's ALL the loop would test.

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

    sorry, that's whether i > 3

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    The idea is to give a person 3 chances to answer, then exit, so my plan would be: assign a variable to track how many chances they've had if they haven't already had all their chances get a guess add one to the number of chances they've had, because they're getting one now if the guess is wrong, tell them it's wrong if the guess is right, tell them it's right if they've had all their chances tell them they lose

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    sorry, the line if they haven't already had all their chances should probably actually be while they haven't already had all their chances

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    actually, I'd also add a line after the if their guess is right condition to increase the number of chances to the max, just so they don't get asked again.

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    then the if they've had all their chances should probably be if they've had all their chances and haven't gotten it right but then you'd need a variable to keep track of if they've gotten it right... Maybe just not tell them if they lose and end after the while loop stops.

  10. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    How about something like this (untested, of course): good = False name = "Monty" i = 0 while (not good) and (i < 3): guess = raw_input("Guess my name") good = (guess == name) if not good: print "NOPE" if good: print "Wow--Are you telepathic or something?"

  11. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Oops. Forgot to increment i in the loop. Sorry--but I told you it was untested. :)

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you, I did get it to work, using your suggestions. By telling them NOPE outside the the loop, it stopped. So is there a way to tell the program just stop, you're done, don't execute any more commands? And I take it there's no such thing as GOTO anymore? guess = "" name = "Monty" i = 0 while guess != name and (i < 3): guess = raw_input("Guess my name: ") i = i + 1 if guess == name: print "good guess" if guess != name: print "NOPE. That was your last chance."

  13. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    the command break inside a loop breaks that loop (but not any loops the broken loop is in). I haven't found any GOTO equivalent in Python but my experience with GOTO is that it's the command that allows me to do things I will invariably later regret.

  14. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    "things I will invariably later regret" -- sometimes those also turn out to be the things we will brag about to our grandchildren... maybe not with goto though.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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.