Rastian
Rastian

Reputation: 13

Issue with a function in basic python calculator and not sure how to fix it

I'm rather new to programming and wanted to make a simple calculator as practice, but I cannot seem to get a function to work the way I would like, and I'm sure it's because I'm missing something, but I'm not exactly sure what. I've tried to look for some answers but I either didn't quite understand or didn't know how to implement it. In the code I wrote, if I remove ask() and replace it with the code I defined it as, it works, but when I use ask(), it does not and gives an error stating that "name 'x' is not defined". Any help is appreciated, along with any tips or ways I could improve this. Thanks in advance.

    def Add(x, y):
        print("Adding %d and %d" % (x, y))
        print("Answer:", x + y)

    def Subtract(x, y):
        print("Subtracting %d from %d" % (y, x))
        print("Answer:", x - y)

    def Multiply(x, y):
        print("Multiplying %d and %d" % (x, y))
        print("Answer:", x * y)

    def Divide(x, y):
        print("Dividing %d from %d" % (y, x))
        print("Answer:", x / y)

    def ask():
         print("Which two numbers would you like to use?")
         x = input(">")
         y = input(">")

    print("Would you like to (a)dd, (s)ubtract, (m)ultiply, or (d)ivide two numbers?")

    op = input(">")

    if op == "a":
        ask()
        Add(x, y)

    elif op == "s":
        ask()
        Subtract(x, y)

    elif op == "m":
        ask()
        Multiply(x, y)

    elif op == "d":
        ask()
        Divide(x, y)

     else:
         print("I don't know what that means.")
         print("\n")

Upvotes: 1

Views: 86

Answers (2)

James Mertz
James Mertz

Reputation: 8759

Can I make a few suggestions (In addition to your issue at hand)?:

# use these as templates
str_fun_cal = "{}: {} with {}"
str_res = "Answer: {}"

# Look at the cleanliness of this!!!!
# Also these actually do what they say they do instead of just printing the result out.  Useful for further
# functionality
def Add(x, y):
    return x + y

def Subtract(x, y):
    return x - y

def Multiply(x, y):
    return x * y

def Divide(x, y):
    return x / y

# use a dictionary to select the function the user calls
func_call_dict = {'a':Add, 's':Subtract, 'm':Multiply, 'd':Divide}

# Fixed an issue with not returning any values
def ask():
    print("Which two numbers would you like to use?")
    in_x = float(input(">"))
    in_y = float(input(">"))
    return in_x, in_y

# Printing function to clean things up
def print_fun(fun_call, x, y, res):
    print(str_fun_cal.format(fun_call, x, y))
    print(str_res.format(res))

print("Would you like to (a)dd, (s)ubtract, (m)ultiply, or (d)ivide two numbers?")
op = input(">")

# check to see if in dictionary, if so then execute the corresponding function if not, display error
if op in func_call_dict:
    x, y = ask()
    sel_fun = func_call_dict[op]
    calc_val = sel_fun(x, y)
    print_fun(sel_fun.__name__, x, y, calc_val) # use of `.__name__` returns the name of the function (Add, Subtract, etc,.) 

else:
    print("I don't know what that means.")
    print("\n")

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117856

You should return those values, also you should convert them to float or whatever you'd like so you can perform numerical operations on them.

def ask():
     print("Which two numbers would you like to use?")
     x = float(input(">"))
     y = float(input(">"))
     return (x,y)

Then you can get the values returned from the function like

if op == "a":
    x,y = ask()
    Add(x, y)

Upvotes: 2

Related Questions