Python Forum
if condition inside try / except -- how to??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if condition inside try / except -- how to??
#1
Good afternoon...
Can someone please show me what I am missing. Basically I want to take an argument in and check if it exists within a tuple of valid arguments. If it does GREAT, if not raise an error that tells the user to try again.....

_SCOPE_VARIABLES = ('open', 'read', 'write', 'query', 'close') class command:	def __init__(self, command, scope = 'NONE', **kwargs):	self.command = command	self.scope = scope	try:	self.scope in _SCOPE_VARIABLES	except:	print ('Try again')


Thanks
Reply
#2
It sounds like you want to throw (raise) an exception, not catch (try/except) one. Just have an if block based on your condition and raise an exception from within the block.

By the way, it's weird that you have a default argument for scope which would result in an exception. It would probably be better to just require clients provide their own argument.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 2,170 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  else condition not called when if condition is false Sandz1286 10 9,340 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 14,451 Jun-01-2020, 06:00 PM
Last Post: penahuse

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.