How do I make Python function errors?

Asked 2 years ago, Updated 2 years ago, 83 views

I am a freshman in college who entered Python. I tried to express the dead rule operation as a function, but the first elif keeps showing an error window called invalid syntax with a red line. I don't know what's wrong. Please help me

def number_AO(choice, num1, num2):

    if choice == 1:
        add = num1 + num2
        print(num1, "+", num2, "=", add)
    return add

    elif choice == 2:
        sub = num1 - num2
        print(num1, "-", num2, "=", sub)
    return sub

    elif choice == 3:
        mul = num1 * num2
        print(num1, "X", num2, "=", mul)
    return mul

    elif choice == 4:
        div = num1 / num2
        print(num1, "/", num2, "=", div)
    return div

    else:
        print ("Selections that do not exist")
    return 

print("Choose the calculation method.")

choice = int (input ("1. addition, 2. subtraction, 3. multiplication, 4. division"))

num1 = int (input ("First integer input: ")))

num2 = int (input ("2nd integer input: ")))

print(number_AO(choice, num1, num2))

python function error

2022-09-22 13:59

1 Answers

Python is an indentation-sensitive language. So indentation is grammar.

The return syntax should be indented as shown below.

def number_AO(choice, num1, num2):

    if choice == 1:
        add = num1 + num2
        print(num1, "+", num2, "=", add)
        return add

    elif choice == 2:
        sub = num1 - num2
        print(num1, "-", num2, "=", sub)
        return sub

    elif choice == 3:
        mul = num1 * num2
        print(num1, "X", num2, "=", mul)
        return mul

    elif choice == 4:
        div = num1 / num2
        print(num1, "/", num2, "=", div)
        return div

    else:
        print ("Selections that do not exist")
    return



2022-09-22 13:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.