Skip to content
Advertisement

If statement not executing. Python

I am still new to programming and I wanted to do a simple calculator in python. However, I could only reach this point of my code:

JavaScript

Not sure why my if statement is not executing after all the correct inputs from the user. I mostly focused on the error handling part and after everything going well, the if statement is just not executing after that. There could probably be a simple mistake but even I can’t understand what’s going on here.

Advertisement

Answer

You have an issue with type casting. So when you are taking input from the user all the userInput is str not int, so you need to cast it like userInput = int(userInput) before doing further calculator operations. Also, you need to assign the float casting to a variable like this variableA = float(variableA) and variableB = float(variableB) otherwise your add/sub/divide/multiply etc. will not do expected operations.

For example add will do concatenation i.e '2' + '4' = 24 not 2 + 4 = 6.0

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement