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

Consider: While = looping construct, If = decision construct. Currently you have the roles reversed. Try approaching from the angle that the While loop is going to do the iteration, and the if else construct will handle the logic to break the loop (i = 4)

OpenStudy (anonymous):

Thanks, that's exactly what I needed. Your suggestion gives me a useful way to conceptualize the problem.

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
Sock: 2.34 + -2/7 + (-0.45) + 3/8 / 4
58 seconds ago 4 Replies 0 Medals
KamieQueen: What exactly is gravity?
22 minutes ago 12 Replies 2 Medals
dukedadon: how to deal with a room full of annoying females?
12 minutes ago 24 Replies 4 Medals
dukedadon: i hope everyone's day is going well :D
29 minutes ago 17 Replies 1 Medal
TETSXPREME: Help
6 minutes ago 17 Replies 1 Medal
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!