Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (anonymous):

Alright, this is my code for ps3 problem 1, and I don't like that double nested ifs. Can anyone suggest me a way to avoid this? http://www.ideone.com/Oq7Mr

OpenStudy (anonymous):

You can use a "for in range" statement instead of the first "if". I'll attach my code (which didn't use the "find" function at all...not sure if I was compelled to do so or not....)

OpenStudy (anonymous):

Nice! Two different ways to do it, 2 that work quite effectively. Well, I thought I was going to post mine. :( But there is a bug it seems, unfound until now.

OpenStudy (anonymous):

If they bother you you could use an "and" and an "elif"... if found == -1 and point == 0: "do whatever" elif .....

OpenStudy (anonymous):

http://dpaste.com/572894/ it is quite similar to yours but esentially the difference is that I used a list. (bug fixed).

OpenStudy (anonymous):

or this: if point - found = -1: "do whatever" elif found == -1 and point != 0: "do whatever else"

OpenStudy (anonymous):

Neat solutions. Since I'm already using find anyway, I'll try out Joz's way. BTW black, when I tried your first solution, it gave me a infinite loop. :P

OpenStudy (anonymous):

Not sure how my first answer would give you an infinite loop... It works fine when I used it. http://dpaste.com/574570/ but in reality there was nothing wrong with your nested "ifs" to begin with.. just giving you an alternative you requested:)

OpenStudy (anonymous):

Nvm, it's because of this line in my original else clause, point = found + 1 That, and a badly implemented if/else. So my bad really :P Thanks.

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!
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!