dear list,
what i have to do is, i have perform some mathmatical operation in one function and using its results as input into another function, here is my code of what i had done till now :

import math
def Math(x1,y1):
    a = x1+y1
    b = x1-y1
    c = x1*y1
    d = x1/y1   
    print "additin is: ", a
    print "subtraction is:", b 
    print "multiplication is:", c 
    print "division is: ", d
    print "square is:", e
    print "square root is", f 

x1 = 0
x = raw_input("enter first number: ")
while not (x.isdigit()):
    x = raw_input("enter a valid number: ")
x1 = int(x)

y1 = 0
y = raw_input("enter second number: ")
while not (y.isdigit()):
    y = raw_input("enter a valid number: ")
y1 = int(y)

Math(x1,y1)

def Manipulator():
        a1 = raw_input("choose the first number among a, b, c, d: ")
        a2 = raw_input("choose the second number among a, b, c, d: ")
        if a1 =='a':
            #here i have to assign the value of a from function Math() to a1

and in the similar way if user choose 'c' as second number then i have to assign the value of c from Math() to a2,
i am unable to do this, i dont know how to call a value from one function to another function, hope you understand what i am trying to ask.
Waiting for your reply.

Dani AI

Generated

The thread already points in the right direction: and correctly advised returning values from the computation function, and highlighted the division-by-zero risk. The current code works but is fragile: heavy if/elif chains, use of int() (which in Python 2 causes integer division), and exec are all maintainability or safety concerns. A cleaner pattern is to make the math function "pure" (no input/output), have it return a structured result, and map the user's single-letter choices to those named results.

A compact, robust approach is to return a dictionary of named results and provide a simple mapping from the user's choice letter to the dict key. This avoids exec, repetitive if chains, and makes later code easier to test and reuse.

import math

def compute_results(x, y):
    x = float(x); y = float(y)
    return {
        'add': x + y,
        'sub': x - y,
        'mul': x * y,
        'div': None if y == 0 else x / y,
        'x_sq': x * x,
        'y_sq': y * y,
        'x_sqrt': None if x < 0 else math.sqrt(x),
        'y_sqrt': None if y < 0 else math.sqrt(y),
    }

letter_to_key = {'a':'add','b':'sub','c':'mul','d':'div','e':'x_sq','f':'x_sqrt'}

Selecting two values then becomes a few safe lookups rather than many branches:

r = compute_results(x_input, y_input)
k1, k2 = letter_to_key.get(choice1), letter_to_key.get(choice2)
n1, n2 = r.get(k1), r.get(k2)

Additional notes: handle invalid letters explicitly; choose how to represent an undefined division or sqrt (None, Exception, or a descriptive string); cast inputs to float to avoid integer truncation (or run under Python 3 where input()// behave as expected). Avoid exec for security and readability. Splitting input/validation, computation, and display into separate functions keeps the code easier to test and extends well if more operations are needed.

Recommended Answers

All 7 Replies

Use function with parameters and return statement

Whoa, whoa whoa, you'll have to redesign it.
First, rename your vars, I haven't, but you should.
Second, have Math return, then print, I've done this for you
Third, lowecase your vars, that's style.

import math
def Math(x1,y1):
    a = x1+y1
    b = x1-y1
    c = x1*y1
    d = x1/y1
    return (a,b,c,d)
x1 = 0
x = raw_input("enter first number: ")
while not (x.isdigit()):
    x = raw_input("enter a valid number: ")
    x1 = int(x)
    y1 = 0
    y = raw_input("enter second number: ")
while not (y.isdigit()):
    y = raw_input("enter a valid number: ")
    y1 = int(y)
a,b,c,d = Math(x1,y1) #unpacking of vars
print "additin is: ", a
print "subtraction is:", b
print "multiplication is:", c
print "division is: ", d
print "square is:", e
print "square root is", f
def Manipulator():
    a1 = raw_input("choose the first number among a, b, c, d: ") #got to rename
    a2 = raw_input("choose the second number among a, b, c, d: ")
    exec("a1="+a1) #fancy shmancy no ifs
    exec("a2="+a2)

Square and square root of what?

i just forget to make it comment, it was nothing just bymistake :(

this is how i come to complete what i was trying to do, i know this is not the right way to do, but i have no one whom i ask so i am asking you guys please mention my mistakes so that i can again put my effort to make it proper, my code is:

import math

def Math(x1,y1):
    a = x1+y1
    b = x1-y1
    c = x1*y1
    d = x1/y1
    while True:
        e1 = raw_input("in Math() which number you want for square x or y: ")
        if e1 == 'x':
            e = x1**2
            break
        elif e1 == 'y':
            e = y1**2
            break
        else:
            e1 = raw_input("please enter the valid entry x or y :") 
    while True:    
        f1 = raw_input("in Math() which number you want for square root x or y: ") 
        if f1 == 'x':
            f = math.sqrt(x1)
            break
        elif f1 == 'y':
            f = math.sqrt(y1)
            break
        else:
            f1 = raw_input("please enter the valid entry x or y :")
    return (a,b,c,d,e,f)

x1 = 0
x = raw_input("enter first number for Math(): ")
while not (x.isdigit()):
    x = raw_input("enter a valid number for Math: ")
x1 = int(x)
y1 = 0
y = raw_input("enter second number: ")
while not (y.isdigit()):
    y = raw_input("enter a valid number: ")
y1 = int(y)
a,b,c,d,e,f = Math(x1,y1) #unpacking of vars 


def Manipulator():
    while True:
        a1 = raw_input("For Manipulator() choose the first number among a,b,c,d,e,f: ")  
        if a1 == 'a':
            n1 = int(a)
            break
        elif a1 == 'b':
            n1 = int(b)
            break
        elif a1 == 'c':
            n1 = int(c)
            break
        elif a1 == 'd':
            n1 = int(d)
            break
        elif a1 == 'e':
            n1 = int(e)
            break
        elif a1 == 'f':
            n1 = int(f)
            break
        else:
            a1 = raw_input("For Manipulator() choose the fir number among a,b,c,d,e,f: ")
    while True:
        a2 = raw_input("For Manipulator() choose the second number among a,b,c,d,e,f: ")
        if a2 == 'a':
            n2 = int(a)
            break
        elif a2 == 'b':
            n2 = int(b)
            break
        elif a2 == 'c':
            n2 = int(c)
            break
        elif a2 == 'd':
            n2 = int(d)
            break
        elif a2 == 'e':
            n2 = int(e)
            break
        elif a2 == 'f':
            n2 = int(f)
            break
        else:
            a2 = raw_input("For Manipulator() choose the sec number among a,b,c,d,e,f: ")

    add = n1+n2
    sub = n1-n2
    mul = n1*n2
    div = n1/n2
    while True:
        sq1 = raw_input("In Manipulator() which number you want for square i or j: ")
        if sq1 == 'i':
            sq = n1**2
            break
        elif sq1 == 'j':
            sq = n2**2
            break
        else:
            sq1 = raw_input("please enter the valid entry i or j :") 
    while True:    
        sqt1 = raw_input("In Manipulator() which number you want for square root i or j: ") 
        if sqt1 == 'i':
            sqt = math.sqrt(n1)
            break
        elif sqt1 == 'j':
            sqt = math.sqrt(n2)
            break
        else:
            sqt1 = raw_input("please enter the valid entry i or j :")


    print "\n addtion for Manipulator() is: ",add
    print "subtraction for Manipulator() is: ",sub
    print "multiplication for Manipulator() is: ",mul
    print "division for Manipulator() is: ",div
    print "square for Manipulator() is:", sq
    print "square root for Manipulator() is", sqt

    return (add,sub,mul,div)
Manipulator()

print "\n additin for Math() is: ", a
print "subtraction for Math() is:", b
print "multiplication for Math() is:", c
print "division for Math() is: ", d 
print "square for Math() is:", e
print "square root for Math() is", f



#and here is the output:

enter first number for Math(): 3
enter second number: 4
in Math() which number you want for square x or y: x
in Math() which number you want for square root x or y: y
For Manipulator() choose the first number among a,b,c,d,e,f: d
For Manipulator() choose the second number among a,b,c,d,e,f: c
In Manipulator() which number you want for square i or j: i
In Manipulator() which number you want for square root i or j: j


 addtion for Manipulator() is:  12
subtraction for Manipulator() is:  -12
multiplication for Manipulator() is:  0
division for Manipulator() is:  0
square for Manipulator() is: 0
square root for Manipulator() is 3.46410161514


 additin for Math() is:  7
subtraction for Math() is: -1
multiplication for Math() is: 12
division for Math() is:  0
square for Math() is: 9
square root for Math() is 2.0

this program will give error if i enter 0 for y, and the error is :
ZeroDivisionError: integer division or modulo by zero
i am unable to found any solution, please suggest some appropriate way to resolve this problem.

When you try to divide by zero ...

x = 2
y = 0
print(x/y)

''' result ...
Traceback (most recent call last):
  File "Untitled", line 3
    print(x/y)
ZeroDivisionError: division by zero
'''

You could protect yourself this way ...

x = 2
y = 0
if y != 0:
    print(x/y)
else:
    print("Division by zero error")
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.