473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to create my own exception for brackets?

i want to create my own exception with code saying whether or not someone has entered a ) after a (, for example (jdduu) is ok, but hkbvbhb)hkhb(, is not, how would i start
Dec 3 '10 #1
4 2206
bvdet
2,851 Expert Mod 2GB
First you would create an algorithm to determine if the user entered unbalanced parentheses. You don't really need a user-defined exception. Just raise an exception based on a certain condition. But if you want to:
Expand|Select|Wrap|Line Numbers
  1. class UnbalancedParenthesesError(Exception):
  2.     pass
  3.  
  4. class NoParenthesesError(Exception):
  5.     pass
  6.  
  7. def getinput(prompt="Enter input enclosed by parentheses"):
  8.     s = raw_input(prompt)
  9.     i = s.find("(")
  10.     j = s.find(")")
  11.     if i < 0 or j < 0:
  12.         raise NoParenthesesError("You left off the parentheses")
  13.     elif j < i:
  14.         raise UnbalancedParenthesesError("Your parentheses are unbalanced")
  15.     return s
  16.  
  17. print getinput()
Dec 3 '10 #2
this method works for single parentheses for example (hcvhkdb), but not for parentheses within parentheses (jcdbjkbdcjb(238))
Dec 4 '10 #3
bvdet
2,851 Expert Mod 2GB
My example was never intended to be a solution. It was intended to be an example of a user-defined exception.
Dec 4 '10 #4
your solution helped alot wit another exception i was working on, but im going to continue to try the brackets, will post if i come up with something
Dec 4 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stefan Gangefors | last post by:
I'm trying to figure out what I'm doing wrong when using ereg(). This is my regexp: ereg("^]$", "]"); and that does'n work, but this does: ereg("^$", "[");
8
by: Ken in Melbourne Australia | last post by:
If I use the curly bracket syntax (referred to as the complex syntax) within a string, how do I get to call a function within it? The php manual says that the first (or previous) character for...
3
by: Robert Mark Bram | last post by:
Howdy All! Is there any difference between referencing objects and attributes with dot notation or bracket notation? Example, document.formname.controlname document Can I access...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
2
by: Robert Hanson | last post by:
Hi All, I noticed that when I place the ending bracket on a function, the enclosed statements will bold for a short period of time. Is there a way to get that to occur again after you have...
3
by: dink | last post by:
helo, I'm new to VS 7.1, searched thru help but no luck. Anyone can tell me a keyboard shortcut to go to the ending bracket when cursor is under opening bracket and vice-versa. I think there...
1
by: Richard Thayne | last post by:
In VS.NET 2003 IDE is there a keyboard shortcut to go to the next bracket or find close bracket? Is there a way to do that and if so is there a website that shows all those keyboard shortcuts? ...
3
by: scunnybunny | last post by:
What I am trying to do is get the program to look along the array and if the first letter is ‘T’ and the last letter ‘M’ (10P7-TXL/) on the highlighted piece of tax code. #write tax bracket 1 ...
11
by: -Lost | last post by:
I was perusing some code and saw: var theForm = document.forms; if (!theForm) { theForm = document.aspnetForm; } Is this a waste of code? Or is there some instance where bracket notation...
1
by: prodziedzic | last post by:
I want use scanf to read from input everything till right square bracket ']'. It seems to be something like that: scanf("%]) but that's not working. Any ideas?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.