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..

Recommended Answers

All 2 Replies

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

do you want to call a function within a function?

def PrintA():
    print "a"

def CallPrintA():
    PrintA()

CallPrintA()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.