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:

import operator as op
print("Greetings user, welcome to the calculator program.nWe offer a list of functions:")
print("1. Addn2. Subtractn3. Multiplyn4. Dividen5. Modulusn6. Check greater number")
while True:
    userInput = input("Please choose what function you would like to use based on their numbers:")
    if userInput.isdigit():
        if int(userInput) in range(1,7):
            str(userInput)
            break
        else:
            print("Number inputted is either below or above the given choices")
            continue
    else:
        print("Incorrect input. Please try again.")
        continue

def add(x,y):
    return op.add(x,y)

def sub(x,y):
    return op.sub(x,y)

def mul(x,y):
    return op.mul(x,y)

def div(x,y):
    return op.truediv(x,y)

def mod(x,y):
    return op.mod(x,y)

def gt(x,y):
    if x == y:
        return "Equal"
    else:
        return op.gt(x,y)

variableA = 0
variableB = 0

while True:
    variableA = input("Enter the first value: ")
    if variableA.isdigit():
        float(variableA)
        break
    else:
        print("Incorrect input. Please try again.")
        continue

while True:
    variableB = input("Enter the second value: ")
    if variableB.isdigit():
        float(variableB)
        break
    else:
        print("Incorrect input. Please try again.")
        continue
    
if userInput == 1:
    print("You chose to add the two numbers and the result is:")
    print(add(variableA,variableB))
    print("Thank you")
elif userInput == 2:
    print("You chose to subtract with the two numbers and the result is:")
    print(sub(variableA,variableB))
    print("Thank you")
elif userInput == 3:
    print("You chose to multiply the two numbers and the result is:")
    print(mul(variableA,variableB))
    print("Thank you")
elif userInput == 4:
    print("You chose to divide with the two numbers and the result is:")
    print(div(variableA,variableB))
    print("Thank you")
elif userInput == 5:
    print("You chose to find the modulo with the two numbers and the result is:")
    print(mod(variableA,variableB))
    print("Thank you")
elif userInput == 6:
    print("Is the first input greater than the second?")
    if sub(variableA,variableB) == True:
        print(f"{sub(variableA,variableB)}. {variableA} is greater than {variableB}")
    elif sub(variableA,variableB) == False:
        print(f"{sub(variableA,variableB)}. {variableB} is greater than {variableA}")
    else:
        print(f"It is {sub(variableA,variableB)}")
    print("Thank you")

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

import operator as op
print("Greetings user, welcome to the calculator program.nWe offer a list of functions:")
print("1. Addn2. Subtractn3. Multiplyn4. Dividen5. Modulusn6. Check greater number")
while True:
    userInput = input("Please choose what function you would like to use based on their numbers:")
    if userInput.isdigit():
        if int(userInput) in range(1,7):
            str(userInput)
            break
        else:
            print("Number inputted is either below or above the given choices")
            continue
    else:
        print("Incorrect input. Please try again.")
        continue

def add(x,y):
    return op.add(x,y)

def sub(x,y):
    return op.sub(x,y)

def mul(x,y):
    return op.mul(x,y)

def div(x,y):
    return op.truediv(x,y)

def mod(x,y):
    return op.mod(x,y)

def gt(x,y):
    if x == y:
        return "Equal"
    else:
        return op.gt(x,y)

variableA = 0
variableB = 0

while True:
    variableA = input("Enter the first value: ")
    if variableA.isdigit():
        variableA = float(variableA)  # <-- fix this line
        break
    else:
        print("Incorrect input. Please try again.")
        continue

while True:
    variableB = input("Enter the second value: ")
    if variableB.isdigit():
        variableB = float(variableB)  # <-- fix this line
        break
    else:
        print("Incorrect input. Please try again.")
        continue

userInput = int(userInput)  # <-- fix this line
if userInput == 1:
    print("You chose to add the two numbers and the result is:")
    print(add(variableA,variableB))
    print("Thank you")
elif userInput == 2:
    print("You chose to subtract with the two numbers and the result is:")
    print(sub(variableA,variableB))
    print("Thank you")
elif userInput == 3:
    print("You chose to multiply the two numbers and the result is:")
    print(mul(variableA,variableB))
    print("Thank you")
elif userInput == 4:
    print("You chose to divide with the two numbers and the result is:")
    print(div(variableA,variableB))
    print("Thank you")
elif userInput == 5:
    print("You chose to find the modulo with the two numbers and the result is:")
    print(mod(variableA,variableB))
    print("Thank you")
elif userInput == 6:
    print("Is the first input greater than the second?")
    if sub(variableA,variableB) == True:
        print(f"{sub(variableA,variableB)}. {variableA} is greater than {variableB}")
    elif sub(variableA,variableB) == False:
        print(f"{sub(variableA,variableB)}. {variableB} is greater than {variableA}")
    else:
        print(f"It is {sub(variableA,variableB)}")
    print("Thank you")
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement