Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 42 Online
OpenStudy (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'

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (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.'

OpenStudy (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.

OpenStudy (anonymous):

sorry, that's whether i > 3

OpenStudy (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

OpenStudy (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

OpenStudy (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.

OpenStudy (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.

OpenStudy (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?"

OpenStudy (anonymous):

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

OpenStudy (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."

OpenStudy (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.

OpenStudy (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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
Breathless: womp
13 minutes ago 0 Replies 0 Medals
Breathless: yo who wanna match pfp?
16 minutes ago 11 Replies 1 Medal
Ylynnaa: This was long time ago lmk if u fw itud83dude1d
4 hours ago 17 Replies 2 Medals
abound: Wow question cove really fell off
6 hours ago 6 Replies 1 Medal
ayden09: chat i love black pink hehe i like jones to
5 hours ago 20 Replies 2 Medals
kamani7676: help
1 day ago 5 Replies 1 Medal
kamani7676: Help
1 day ago 76 Replies 2 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!