Why is my code returning my else: statement?

Vine

When i run through my calculator, it gives the following results;

Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4):3
Enter first number: 1
Enter second number: 5
Invalid! Input

Can anyone explain to me why it is responding with my else if statement, i'v checked the code many times, plus i have copied paste the code directly, as is, after much frustration, yet it yields the same result?

# A simple calculator that can add, subtract, multiply and divide.

# define functions
def add(x, y):
 """This function adds two numbers"""
 return x + y

def subtract(x, y):
 """This function subtracts two numbers"""
 return x - y

def multiply(x, y):
 """This function multiplies two numbers"""
 return x * y 

def divide(x, y):
 """This function divides two numbers"""
 return x / y 


# Take input from the user
print ("Select operation.")
print ("1.Add")
print ("2.Subtract")
print ("3.Multiply")
print ("4.Divide")

choice = input("Enter choice(1/2/3/4):")

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

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

elif choice == '2':
    print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
    print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
    print(num1,"/",num2,"=", divide(num1,num2))

else:
    print("Invalid! Input")
Cyphase

You're using Python 2, where input() evaluates what is entered; so when you enter 2, for instance, choice contains the int 2. Try entering '2' into your current code (including the quotes). It'll act as you expect entering 2 to act.

You should use raw_input() on Python 2 and input() on Python 3. If you want your code to be compatible with both, you can use the following code, after which you can always just use input():

try:
    input = raw_input  # Python 2
except NameError:  # We're on Python 3
    pass  # Do nothing

You can also use the six package, which does this and many other Python 2/3 compatibility things.

In Python 3 input() does what raw_input() does in Python 2, and Python 2's input() is gone.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is my code bypassing my else if statement?

From Dev

Why is my if statement skipping my else statement?

From Dev

Why is my if statement skipping the else?

From Dev

Why is this if statement in my code ignored?

From Dev

Why is my Java for/if/else statement outputting duplicates

From Dev

Why is my IF ELSE statement defaulting backwards?

From Dev

why is my javascript if else statement not working?

From Dev

Why is my if else statement being ignored

From Dev

Why is my if else statement not working? jquery

From Dev

Why isn't my else statement working?

From Dev

Why does my if statement work, but my else doesn't?

From Dev

Why is my elif being treated as an else statement in my bash script?

From Dev

Why is my if-else-if statement not working in my arraylist?

From Java

Why is my Jsoup Code not Returning the Correct Elements?

From Dev

why my code is returning 403 forbidden acess

From Dev

Why is my code returning it's results in quotes?

From Dev

Why is my code returning an incorrect value?

From Dev

Why does my code step into an else-if statement whose condition should be false?

From Dev

Why does my code step into an else-if statement whose condition should be false?

From Dev

Why is my code skipping straight to else?

From Dev

Why is my else statment running the code twice

From Dev

Why is my code skipping straight to else?

From Dev

Why is my code skipping through the if statement?

From Java

Why does a while loop work, but not an if statement in returning my values?

From Dev

How can I use shorthand if else statement in my js code

From Dev

Can i use shortand if else statement in my code

From Dev

Why does a Boolean variable prevent my else statement from printing?

From Dev

Why my if else statement (i.e. ? : ) does not work?

From Dev

Why isn't my if, elseif, else statement working?

Related Related

  1. 1

    Why is my code bypassing my else if statement?

  2. 2

    Why is my if statement skipping my else statement?

  3. 3

    Why is my if statement skipping the else?

  4. 4

    Why is this if statement in my code ignored?

  5. 5

    Why is my Java for/if/else statement outputting duplicates

  6. 6

    Why is my IF ELSE statement defaulting backwards?

  7. 7

    why is my javascript if else statement not working?

  8. 8

    Why is my if else statement being ignored

  9. 9

    Why is my if else statement not working? jquery

  10. 10

    Why isn't my else statement working?

  11. 11

    Why does my if statement work, but my else doesn't?

  12. 12

    Why is my elif being treated as an else statement in my bash script?

  13. 13

    Why is my if-else-if statement not working in my arraylist?

  14. 14

    Why is my Jsoup Code not Returning the Correct Elements?

  15. 15

    why my code is returning 403 forbidden acess

  16. 16

    Why is my code returning it's results in quotes?

  17. 17

    Why is my code returning an incorrect value?

  18. 18

    Why does my code step into an else-if statement whose condition should be false?

  19. 19

    Why does my code step into an else-if statement whose condition should be false?

  20. 20

    Why is my code skipping straight to else?

  21. 21

    Why is my else statment running the code twice

  22. 22

    Why is my code skipping straight to else?

  23. 23

    Why is my code skipping through the if statement?

  24. 24

    Why does a while loop work, but not an if statement in returning my values?

  25. 25

    How can I use shorthand if else statement in my js code

  26. 26

    Can i use shortand if else statement in my code

  27. 27

    Why does a Boolean variable prevent my else statement from printing?

  28. 28

    Why my if else statement (i.e. ? : ) does not work?

  29. 29

    Why isn't my if, elseif, else statement working?

HotTag

Archive