Re: [Tutor] Control flow
Thanks for the enthusiasm on how input/raw_input() works - my original intention was to ask a question on control flow so I didn't spend that much time testing out this piece of input code besides typing. But I did learn a lot. Thanks! Gilbert Jacob S. wrote: I noticed that too, Liam. b = input(Weather is really bad, still go out to jog? [y/n]) # Would it kill you to have whitespace in a prompt? should really be b = raw_input(Weather is really bad, still go out to jog? [y/n]) to get the effect he wants. input() doesn't only take integers, it takes valid python objects. Integers are objects, but so are lists, dictionaries, tuples, actually it takes everything, BUT!!! it trys to return a valid python object for input. So it will take a string--don't quote me on this--if you explicitly put the string in quotes. If you don't put the string in quotes, it instead searches the namespaces for that object. So say the user typed in bad_weather when the interpreter gave that prompt. Then, b == y evaluates true because bad_weather == y. Did I explain it right? Or am I trying to explain something you already know? I know I get frustrated when people try to explain concepts that I already know... HTH, Jacob Schmidt erk, to the list, to the List! if ( bad_weather =='y' ): # ask user only if weather is bad. b = input ( Weather is really bad, still go out to jog?[y/n] ) if b == 'y': go_jogging() Anyone else notice that he's never gonna go jogging if the weather is bad? Unless I've got input() wrong, it only takes integers... ? Regards, Liam Clarke -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Control flow
Gilbert Tsang wrote: Hi there, I have this logic that I cannot wrap my mind it: def go_jogging(): # go out and jog return if ( bad_weather =='y' ): # ask user only if weather is bad. b = input ( Weather is really bad, still go out to jog?[y/n] ) if b == 'y': go_jogging() else: # program should exit now else: go_jogging() I can't get the program to stop processing further in the middle (apparently neither exit nor goto-label exist in Python, sorry for the C++ mindset) so I used exception to achieve what I want. I know in that example you could probably manipulate the logic so that program ends at the bottom of the if-tree. My question is then how to exit in the middle of a if-then-else tree? Thanks, Gilbert. try: if ( bad_weather =='y' ): b = input ( Weather is really bad, still go out to jog?[y/n] ) if b == 'y': go_jogging() else: raise Exception( quit ) else: go_jogging() except Exception, inst: print Program exits now ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Well, if its in a function you can just do return instead of raise Exception(quit) since the function exits once it returns something. It's sort of the same as the following: Say you have a recursive function fact(n) which takes in a number and returns the factorial of that number. One way to code this would be: def fact(n): if n 3: return n*fact(n-1) else: return 2*n However, since the function 'exits' after a return statement, it saves space in general to do the following: def fact(n): if n 3: return n*fact(n-1) return 2*n Because the only way return 2*n will be reached is if n is smaller than or equal to three, which is what we wanted anyway. BTW, the way this function works is like this: Let's say you want to calculate fact(10). 10 is greater than 3, so the program calculates 10*fact(9) and so on until fact(3) is reached, at which point it will return 10*(9*(8*(7*(6*(5*(4*(6))) (6 is 2 * 3). or the factorial of 10. I probably didn't do too good a job explaining that, and that's probably not the most efficient implementation (it breaks @ around fact(1000) by default, and around fact(11462) when you use sys.setrecursionlimit(2000) (11462 is as high as it'll go on my machine)), so please feel free to ask questions. HTH, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Control flow
Gilbert Tsang wrote: Hi there, I have this logic that I cannot wrap my mind it: def go_jogging(): # go out and jog return if ( bad_weather =='y' ): # ask user only if weather is bad. b = input ( Weather is really bad, still go out to jog?[y/n] ) if b == 'y': go_jogging() else: # program should exit now else: go_jogging() I can't get the program to stop processing further in the middle (apparently neither exit nor goto-label exist in Python, sorry for the C++ mindset) so I used exception to achieve what I want. I know in that example you could probably manipulate the logic so that program ends at the bottom of the if-tree. My question is then how to exit in the middle of a if-then-else tree? Thanks, Gilbert. try: if ( bad_weather =='y' ): b = input ( Weather is really bad, still go out to jog?[y/n] ) if b == 'y': go_jogging() else: raise Exception( quit ) else: go_jogging() except Exception, inst: print Program exits now ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Well, if its in a function you can just do return instead of raise Exception(quit) since the function exits once it returns something. It's sort of the same as the following: Say you have a recursive function fact(n) which takes in a number and returns the factorial of that number. One way to code this would be: def fact(n): if n 3: return n*fact(n-1) else: return 2*n However, since the function 'exits' after a return statement, it saves space in general to do the following: def fact(n): if n 3: return n*fact(n-1) return 2*n Because the only way return 2*n will be reached is if n is smaller than or equal to three, which is what we wanted anyway. BTW, the way this function works is like this: Let's say you want to calculate fact(10). 10 is greater than 3, so the program calculates 10*fact(9) and so on until fact(3) is reached, at which point it will return 10*(9*(8*(7*(6*(5*(4*(6))) (6 is 2 * 3). or the factorial of 10. I probably didn't do too good a job explaining that, and that's probably not the most efficient implementation (it breaks @ around fact(1000) by default, and around fact(11462) when you use sys.setrecursionlimit(2000) (11462 is as high as it'll go on my machine)), so please feel free to ask questions. HTH, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Control flow
SEveral solutions here. The best is to restructure the code a little: def go_jogging(): # go out and jog return if not bad_weather == 'y': # where is this initially set BTW? go_jogging() else # ask user only if weather is bad. b = input ( Weather is really bad, still go out to jog?[y/n] ) if b == 'y': go_jogging() Its shorter, simpler and makes the most common case the default (assuming that bad weather is the exception!) I can't get the program to stop processing further in the middle Good, that would be really bad practice from a structured programming point of view. :-) But if you really, really must, you could always call raise SystemExit which bombs out more or less immediately - like exit() in C C++ mindset) so I used exception to achieve what I want. Yes thats ok, and the exception to use is already there... example you could probably manipulate the logic so that program ends at the bottom of the if-tree. My question is then how to exit in the middle of a if-then-else tree? You should never need to. One of the things that structured programming (Edsgar Dijkstra to be precise) showed was that you can *always* rearrange things so that goto's and intermediate exits are not needed and indeed can introduce an extra level of complexity and error. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor