Python Forum
try except in while loops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: try except in while loops (/thread-7728.html)



try except in while loops - mepyyeti - Jan-22-2018

The following code snippet works..
from some_py_file import Thing
#I'm still shaky relying on functools' partial function so for this portion I'm writing out code in 'bulk' style
code = Thing()
some_num = 100
while True:
	question = input('[yes/no]')
	if question =='yes' or 'y':
		try:
			num = float(input('Enter num...'))
			if num ==4 or num == '':
				num = float(some_num /4)
			if num ==5:
				num =float(some_num /5)
			if amount_down == 5:
				num = float(some_num/20)
			num2 = float(input(''))
			print('some_num: ',some_num,'  num: ',num)#just to make sure loop reaches here, with question = 'yes'
			code.func_in_Thing(num, num2)
			break
		except ValueError:
			print('please enter numbers...')
			continue
So the above works...the opposite should also work...but does not.

from some_py_file import Thing
code = Thing()
some_num = 100
while True:
	question = input('[yes/no]')
	if question =='no' or 'n':
		break:#this should kick me out of the while loop ALTOGETHER if question != 'no/n' 
#if question =='foo' it should move the code try/except
#BUT question =='foo' just kicks me out altogether.
	try:
		num = float(input('Enter num...'))
		if num ==4 or num == '':
			num = float(some_num /4)
		if num ==5:
			num =float(some_num /5)
		if amount_down == 5:
			num = float(some_num/20)
		num2 = float(input(''))
		print('some_num: ',some_num,'  num: ',num)
		code.func_in_Thing(num, num2)
		break
	except ValueError:
		print('please enter numbers...')
		continue
The way I understand break it should kick me out of the while ONLY when question == 'no/n'...so why is it the first snippet operates properly and this one breaks regardless of question == 'foo'. if question !='no/n' python SHOULD move down to the try/except code....why doesn't it?


RE: try except in while loops - buran - Jan-22-2018

https://python-forum.io/Thread-Multiple-expressions-with-or-keyword
check line#6


RE: try except in while loops - mepyyeti - Jan-22-2018

I did! sorry about that. Blush I've noticed so much of coding is testing and debugging it's ridiculous!

I'm surprised break is so useful ...did not expect to rely on it SO MUCH!

Also in my nascent experience I've relied on while loops...but most textbooks emphasize while & do-while loops almost equally...in real life when do you guys rely on do while loops (I get the 'practice' examples but what instances would you want to iterate through a loop and then find the variables are false? The instances seem far and few between...


RE: try except in while loops - buran - Jan-22-2018

well, there is no do-while loops
see https://www.python.org/dev/peps/pep-0315/
proposal was rejected back in 2003
I think you are confused...


RE: try except in while loops - mepyyeti - Jan-23-2018

u r right Blush it's a js/php loop (not to mention c++)