-1

When I execute and run the code the program doesn't seem to store the c input, therefore not continuing the code to perform the rest of the calculator function.

def calc():

    print("Press 1 for addition")
    print("Press 2 for subtraction")
    print("Press 3 for multiplication")
    print("Press 4 for division")

    c = input()

    if c == 1:
        print("Enter a number")
        x = input()
        print("Enter another number")
        y = input()
        return x + y

    elif c == 2:
        print("Enter a number")
        x = input()
        print("Enter another number")
        y = input()
        return x - y

    elif c == 3:
        print("Enter a number")
        x = input()
        print("Enter another number")
        y = input()
        return x * y

    elif c == 4:
        print("Enter a number")
        x = input()
        print("Enter another number")
        y = input()
        return x / y

calc()

I've now improved the code but can't seem to get the indentation right and it seems that the return function on each type of math that is being performed is 'outside function'

def calc():
print("Press 1 for addition")
print("Press 2 for subtraction")
print("Press 3 for multiplication")
print("Press 4 for division")

c = int(input())

def get_inputs():
    print("Enter a number")
    x = int(input())
    print("Enter another number")
    y = int(input())
    return x, y

if c == 1:
    x, y = get_inputs()
    return x + y #These return functions seem to be an error

elif c == 2:
    x, y = get_inputs()
    return x - y

elif c == 3:
    x, y = get_inputs()
    return x * y

elif c == 4:
    x, y = get_inputs()
    return x / y

calc()
Adyn_G
  • 7
  • 3

2 Answers2

0

input() in Python 3 is same as raw_input() in Python 2.7 which returns object of str type. You need to explicitly type-cast it to int

 c = int(input())
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
ECM
  • 57
  • 3
0

First, use c = int(input()) instead to convert the input string to integer. Also, just to make the program cleaner, since you're already using functions (calc), might as well put the input part for each operation in a function:

def get_inputs():
    print("Enter a number")
    x = int(input())
    print("Enter another number")
    y = int(input())
    return x, y

And then for each operation do something like:

if c == 1:
    a, b = get_inputs()
    return a + b

(Edit) try this:

def get_inputs():
    print("Enter a number")
    x = int(input())
    print("Enter another number")
    y = int(input())
    return x, y

def calc():
    print("Press 1 for addition")
    print("Press 2 for subtraction")
    print("Press 3 for multiplication")
    print("Press 4 for division")

    c = int(input())

    if c == 1:
        x, y = get_inputs()
        return x + y

    elif c == 2:
        x, y = get_inputs()
        return x - y

    elif c == 3:
        x, y = get_inputs()
        return x * y

    elif c == 4:
        x, y = get_inputs()
        return x / y

calc()
abacles
  • 849
  • 1
  • 7
  • 15