anonymous
  • anonymous
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'
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
The simplest way to fix this code is to insert another test just before the last line: if guess == name: print 'good guess'
anonymous
  • anonymous
Thanks-- what about how to keep it from printing NOPE forever, if three wrong answers are entered, since guess is still != name?
anonymous
  • anonymous
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.'

Looking for something else?

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

More answers

anonymous
  • anonymous
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.
anonymous
  • anonymous
sorry, that's whether i > 3
anonymous
  • anonymous
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
anonymous
  • anonymous
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
anonymous
  • anonymous
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.
anonymous
  • anonymous
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.
anonymous
  • anonymous
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?"
anonymous
  • anonymous
Oops. Forgot to increment i in the loop. Sorry--but I told you it was untested. :)
anonymous
  • anonymous
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."
anonymous
  • anonymous
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.
anonymous
  • anonymous
"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.

Looking for something else?

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