FAQ
is there a general philosophy as to when to use exceptions and when
not to?

like here:
def Calc():
global nbr
try:
print eval(nbr)
except:
print "Not computable"
nbr = ""

i have a calculator and nbr is a string that contains '0123456789+-*/'

if the string ends on +-*/ it will throw an exception(unexpected EOF).

i could easily prevent the exceptions here with an if-statement, is
that preferrable and why?


also when u throw exceptions should u catch the speicfic one? i guess
only if u want it to do soemthing special since catching only only
one exception logicall would abort the program if another one is
thrown?

Search Discussions

  • Cokofreedom at Apr 10, 2008 at 8:51 am
    In general you should only catch the exceptions you want to catch,
    therefore avoiding the issue of catching "unexpected" ones, for
    instances the programming unexpectandly closing.

    Well, exception handling is expensive (when it catches one) so it
    really is up to you. If you are using eval and know it might "EOF"
    then you should probably look to handle that. The main IF statement
    style I can think of (checking the end of the string) wouldn't be much
    of an improvement.

    Currently I would be very worried about seeing that code as it breaks
    a number of "conventions". However it depends on the importance of the
    code to wherever or not you should change this. (Global variable, the
    use of Eval, the CATCH ALL except and the setting of a global variable
    at the end.)

    I've seen a good few (simple and advanced) calculator examples using
    python on the NET, it might be worth looking at some to see their
    style of coding a calculator to help your own.
  • Skanemupp at Apr 10, 2008 at 10:00 am

    On 10 Apr, 10:51, cokofree... at gmail.com wrote:
    In general you should only catch the exceptions you want to catch,
    therefore avoiding the issue of catching "unexpected" ones, for
    instances the programming unexpectandly closing.

    Well, exception handling is expensive (when it catches one) so it
    really is up to you. If you are using eval and know it might "EOF"
    then you should probably look to handle that. The main IF statement
    style I can think of (checking the end of the string) wouldn't be much
    of an improvement.

    Currently I would be very worried about seeing that code as it breaks
    a number of "conventions". However it depends on the importance of the
    code to wherever or not you should change this. (Global variable, the
    use of Eval, the CATCH ALL except and the setting of a global variable
    at the end.)

    I've seen a good few (simple and advanced) calculator examples using
    python on the NET, it might be worth looking at some to see their
    style of coding a calculator to help your own.

    i know about the GLOBAL stuff, i might rewrite the program using
    classes later so i can avoid this. i am mainly playin with tkinter for
    now.

    i was thinking the same check with if at the beginning and end of the
    string so there isnt a */ but also if someone uses /// or soemthing
    like that in the middle it crashes so it is hard to cover every case,
    esp since **5 means ^5.
    is the use of if also expensive?
  • Skanemupp at Apr 10, 2008 at 10:13 am
    here is the whole code:

    from __future__ import division
    import Tkinter
    from Tkinter import *

    mygui = Tkinter.Tk()
    mygui.title("Calculator")

    w = Label(mygui, text="Answer: ")
    w.place(relx=0.15, rely=0.1, anchorÎNTER)

    nbr = ""

    def Disp(nstr):
    global nbr
    nbr=nbr+nstr
    print "You need to seek help!",nbr

    def Calc():
    global nbr
    try:
    print eval(nbr)
    #a = Label(mygui, text=eval(nbr))
    #a.place(relx=0.4, rely=0.1, anchorÎNTER)
    except:
    print "Not computable"
    nbr = ""

    def Erase():
    global nbr
    nbr = ""


    b = Button(mygui, text="1",command=lambda n='1':Disp(n), width=2,
    height=1)
    b.place(relx=0.1, rely=0.2, anchorÎNTER)
    b = Button(mygui, text="2",command=lambda n='2':Disp(n), width=2,
    height=1)
    b.place(relx=0.2, rely=0.2, anchorÎNTER)
    b = Button(mygui, text="3",command=lambda n='3':Disp(n), width=2,
    height=1)
    b.place(relx=0.3, rely=0.2, anchorÎNTER)
    b = Button(mygui, text="+",command=lambda n='+':Disp(n), width=2,
    height=1)
    b.place(relx=0.4, rely=0.2, anchorÎNTER)
    b = Button(mygui, text="4",command=lambda n='4':Disp(n), width=2,
    height=1)
    b.place(relx=0.1, rely=0.3, anchorÎNTER)
    b = Button(mygui, text="5",command=lambda n='5':Disp(n), width=2,
    height=1)
    b.place(relx=0.2, rely=0.3, anchorÎNTER)
    b = Button(mygui, text="6",command=lambda n='6':Disp(n), width=2,
    height=1)
    b.place(relx=0.3, rely=0.3, anchorÎNTER)
    b = Button(mygui, text="-",command=lambda n='-':Disp(n), width=2,
    height=1)
    b.place(relx=0.4, rely=0.3, anchorÎNTER)
    b = Button(mygui, text="7",command=lambda n='7':Disp(n), width=2,
    height=1)
    b.place(relx=0.1, rely=0.4, anchorÎNTER)
    b = Button(mygui, text="8",command=lambda n='8':Disp(n), width=2,
    height=1)
    b.place(relx=0.2, rely=0.4, anchorÎNTER)
    b = Button(mygui, text="9",command=lambda n='9':Disp(n), width=2,
    height=1)
    b.place(relx=0.3, rely=0.4, anchorÎNTER)
    b = Button(mygui, text="*",command=lambda n='*':Disp(n), width=2,
    height=1)
    b.place(relx=0.4, rely=0.4, anchorÎNTER)
    b = Button(mygui, text="0",command=lambda n='0':Disp(n), width=2,
    height=1)
    b.place(relx=0.1, rely=0.5, anchorÎNTER)
    b = Button(mygui, text="C",command=Erase, width=2, height=1)
    b.place(relx=0.2, rely=0.5, anchorÎNTER)
    b = Button(mygui, text="^.5",command=lambda n='**.5':Disp(n), width=2,
    height=1)
    b.place(relx=0.3, rely=0.5, anchorÎNTER)
    b = Button(mygui, text="/",command=lambda n='/':Disp(n), width=2,
    height=1)
    b.place(relx=0.4, rely=0.5, anchorÎNTER)
    b = Button(mygui, text="=",commandÊlc, width=2, height=1)
    b.place(relx=0.2, rely=0.7, anchorÎNTER)

    mygui.mainloop()
  • Cokofreedom at Apr 10, 2008 at 10:38 am

    def Calc():
    global nbr
    try:
    print eval(nbr)
    #a = Label(mygui, text=eval(nbr))
    #a.place(relx=0.4, rely=0.1, anchorÎNTER)
    except:
    print "Not computable"
    nbr = ""

    def Erase():
    global nbr
    nbr = ""
    Seems to me you could be better off passing a parameter and a return
    statement of None (or your parameter cleaned) for those functions,
    which should work. Given an input, Eval it and then return None. That
    way you wouldn't need the Erase...
  • Skanemupp at Apr 10, 2008 at 10:54 am

    On 10 Apr, 12:38, cokofree... at gmail.com wrote:
    def Calc():
    global nbr
    try:
    print eval(nbr)
    #a = Label(mygui, text=eval(nbr))
    #a.place(relx=0.4, rely=0.1, anchorÎNTER)
    except:
    print "Not computable"
    nbr = ""
    def Erase():
    global nbr
    nbr = ""
    Seems to me you could be better off passing a parameter and a return
    statement of None (or your parameter cleaned) for those functions,
    which should work. Given an input, Eval it and then return None. That
    way you wouldn't need the Erase...
    the erase() id alwys need if u wanna abort whilst u wrote something.
  • Cokofreedom at Apr 10, 2008 at 11:15 am
    the erase() id alwys need if u wanna abort whilst u wrote something.
    But if it is meant to only evaluate once you've pressed the enter key
    (I take it?) you shouldn't need that. And if you are to abort while
    evaluating it will not do that.
  • Skanemupp at Apr 10, 2008 at 11:41 am

    On 10 Apr, 13:15, cokofree... at gmail.com wrote:
    the erase() id alwys need if u wanna abort whilst u wrote something.
    But if it is meant to only evaluate once you've pressed the enter key
    (I take it?) you shouldn't need that. And if you are to abort while
    evaluating it will not do that.

    CHANGED IT NOW TO A MUCH BETTER SOLUTION(will see if i can make a
    better solution with the buttons, hate big blocks of similarlooking
    code):


    from __future__ import division
    import Tkinter
    from Tkinter import *

    mygui = Tkinter.Tk()
    mygui.title("Calculator")

    l = Label(mygui, text="Answer: ")
    l.place(relx=0.15, rely=0.2, anchorÎNTER)

    e = Entry(mygui)
    e.place(relx=0.4, rely=0.1, anchorÎNTER)

    def Disp(nstr):
    e.insert(END, nstr)

    def Calc():
    expr=e.get()
    try:
    b = Label(mygui, text=eval(expr))
    b.place(relx=0.4, rely=0.2, anchorÎNTER)
    except:
    b = Label(mygui, text="Not computable")
    b.place(relx=0.4, rely=0.2, anchorÎNTER)

    def Erase():
    e.delete(0,END)

    b = Button(mygui, text="1",command=lambda n='1':Disp(n), width=2,
    height=1)
    b.place(relx=0.1, rely=0.4, anchorÎNTER)
    b = Button(mygui, text="2",command=lambda n='2':Disp(n), width=2,
    height=1)
    b.place(relx=0.2, rely=0.4, anchorÎNTER)
    b = Button(mygui, text="3",command=lambda n='3':Disp(n), width=2,
    height=1)
    b.place(relx=0.3, rely=0.4, anchorÎNTER)
    b = Button(mygui, text="+",command=lambda n='+':Disp(n), width=2,
    height=1)
    b.place(relx=0.4, rely=0.4, anchorÎNTER)
    b = Button(mygui, text="4",command=lambda n='4':Disp(n), width=2,
    height=1)
    b.place(relx=0.1, rely=0.5, anchorÎNTER)
    b = Button(mygui, text="5",command=lambda n='5':Disp(n), width=2,
    height=1)
    b.place(relx=0.2, rely=0.5, anchorÎNTER)
    b = Button(mygui, text="6",command=lambda n='6':Disp(n), width=2,
    height=1)
    b.place(relx=0.3, rely=0.5, anchorÎNTER)
    b = Button(mygui, text="-",command=lambda n='-':Disp(n), width=2,
    height=1)
    b.place(relx=0.4, rely=0.5, anchorÎNTER)
    b = Button(mygui, text="7",command=lambda n='7':Disp(n), width=2,
    height=1)
    b.place(relx=0.1, rely=0.6, anchorÎNTER)
    b = Button(mygui, text="8",command=lambda n='8':Disp(n), width=2,
    height=1)
    b.place(relx=0.2, rely=0.6, anchorÎNTER)
    b = Button(mygui, text="9",command=lambda n='9':Disp(n), width=2,
    height=1)
    b.place(relx=0.3, rely=0.6, anchorÎNTER)
    b = Button(mygui, text="*",command=lambda n='*':Disp(n), width=2,
    height=1)
    b.place(relx=0.4, rely=0.6, anchorÎNTER)
    b = Button(mygui, text="0",command=lambda n='0':Disp(n), width=2,
    height=1)
    b.place(relx=0.1, rely=0.7, anchorÎNTER)
    b = Button(mygui, text="C",command=Erase, width=2, height=1)
    b.place(relx=0.2, rely=0.7, anchorÎNTER)
    b = Button(mygui, text="^",command=lambda n='**':Disp(n), width=2,
    height=1)
    b.place(relx=0.3, rely=0.7, anchorÎNTER)
    b = Button(mygui, text="/",command=lambda n='/':Disp(n), width=2,
    height=1)
    b.place(relx=0.4, rely=0.7, anchorÎNTER)
    b = Button(mygui, text=".",command=lambda n='.':Disp(n), width=2,
    height=1)
    b.place(relx=0.1, rely=0.8, anchorÎNTER)
    b = Button(mygui, text="(",command=lambda n='(':Disp(n), width=2,
    height=1)
    b.place(relx=0.2, rely=0.8, anchorÎNTER)
    b = Button(mygui, text=")",command=lambda n=')':Disp(n), width=2,
    height=1)
    b.place(relx=0.3, rely=0.8, anchorÎNTER)
    b = Button(mygui, text="=",commandÊlc, width=2, height=1)
    b.place(relx=0.4, rely=0.8, anchorÎNTER)

    mygui.mainloop()
  • Skanemupp at Apr 10, 2008 at 12:11 pm
    from __future__ import division
    import Tkinter
    from Tkinter import *

    mygui = Tkinter.Tk()
    mygui.title("Calculator")

    l = Label(mygui, text="Answer: ")
    l.place(relx=0.15, rely=0.2, anchorÎNTER)

    e = Entry(mygui)
    e.place(relx=0.4, rely=0.1, anchorÎNTER)

    def Disp(nstr):
    e.insert(END, nstr)

    def Calc():
    expr=e.get()
    try:
    b = Label(mygui, text=eval(expr))
    b.place(relx=0.4, rely=0.2, anchorÎNTER)
    except:
    b = Label(mygui, text="Not computable")
    b.place(relx=0.4, rely=0.2, anchorÎNTER)

    def Erase():
    e.delete(0,END)


    x = 0.1
    y = 0.4
    for char in '123+456-789*0^./()':
    b = Button(mygui, text=char,command=lambda n=char:Disp(n),
    width=2, height=1)
    b.place(relx=x, rely=y, anchorÎNTER)
    x=x+0.1
    if x==0.5:
    x=0.1
    y=y+0.1

    b = Button(mygui, text="C",command=Erase, width=2, height=1)
    b.place(relx=0.3, rely=0.8, anchorÎNTER)
    b = Button(mygui, text="=",commandÊlc, width=2, height=1)
    b.place(relx=0.4, rely=0.8, anchorÎNTER)

    mygui.mainloop()
  • Skanemupp at Apr 10, 2008 at 12:40 pm

    On 10 Apr, 12:38, cokofree... at gmail.com wrote:
    def Calc():
    global nbr
    try:
    print eval(nbr)
    #a = Label(mygui, text=eval(nbr))
    #a.place(relx=0.4, rely=0.1, anchorÎNTER)
    except:
    print "Not computable"
    nbr = ""
    def Erase():
    global nbr
    nbr = ""
    Seems to me you could be better off passing a parameter and a return
    statement of None (or your parameter cleaned) for those functions,
    which should work. Given an input, Eval it and then return None. That
    way you wouldn't need the Erase...
    i never really got what u meant by this?

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 10, '08 at 8:15a
activeApr 10, '08 at 12:40p
posts10
users2
websitepython.org

2 users in discussion

Skanemupp: 7 posts Cokofreedom: 3 posts

People

Translate

site design / logo © 2022 Grokbase