0

im a newbie here in python and in this forum site. im using python 2.7.1. and im making a simple calculator that can trap if the second input is zero in division 0peration..
is it possible to call def to def within the if-else statement where ?? because im trying to show an another tkinter with label that shows that it is an error in division operation..

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Archenemie
0

It is legal to define local functions that are effectively invisible outside the outer function. But I doubt that is what you mean? I think you are trying to say something about program flow. To "trap" an illegal division, I would raise an exception, and handle it in the main loop:

def do_divide(lhs,rhs):
    if 0 == rhs:
        raise DivideByZeroException # defined somewhere else
    print lhs/rhs
# ...

while True:
    op = getOperation() # defined somewhere else
    if 'q' = op.lower()[0]: 
        break
    lhs = getValue() # defined somewhere else
    rhs = getValue()
    try:
        if '/' == op:
            do_divide(lhs,rhs)
        # elif op is + or - or whatever
    except DivideByZeroException, x:
        print x
0

do you want to call a function within a function?

def PrintA():
    print "a"

def CallPrintA():
    PrintA()

CallPrintA()

Edited by Archenemie: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.