Sign in
Log inSign up

An amateur in need of some help with his code (Python/Pycharm)

Michael Truman Leonardi's photo
Michael Truman Leonardi
·May 2, 2018

Hi, I just started coding pretty recently and I have a bit of problem with my code, I don't seem to understand why. I know this stuff is super easy, but we all got to start somewhere don't we?

My code:

def add(x, y): return (x + y)

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

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

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

def main(): myName = input("Hello, what is your name?") print("Hello",myName,"!")

myFunction = input("What do you want to do?(add, sub, divide, multiply)")
if(myFunction != "add" and "sub" and "divide" and "multiply"):
    print("You must enter a valid operation!")
else:
    var1 = input("Enter number1")
    var2 = input("Enter number2")
    if(myFunction == "add"):
        print(add(var1, var2))

    elif(myFunction == "sub"):
        print(sub(var1, var2))

    elif(myFunction == "divide"):
        print(divide(num1, num2))

    else:
        print(multiply(num1, num2))

main()

.

.

.

Possible outcome 1 :

(entering add as an input)

Hello, what is your name?John

Hello John !

What do you want to do?add

Enter number1(1)

Enter number2(3)

13

.

.

.

Possible outcome 2 :

(entering any other input except add)

Hello,what is your name?John

Hello John !

What do you want to do?sub

You must enter a valid operation!

.

.

.

Why does my code interpret 1 and 3 as a variable and not as a number? Why won't my code run any other outcome unless it's an "add" even though I put an "and" in my code?