Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Need help with python calculator issue (bug?)

User8574

Active Coder
I typed in this code

Python:
operator = input("Choose an operator (1 = Add, 2 = Subtract, 3 = Divide, 4 = Multiply) ")
first = input("Enter an integer ")
second = input("Enter a second integer ")

if operator == 1:
    result = (float(first) + float(second))

elif operator == 2:
    result = (float(first) - float(second))

elif operator == 3:
    result = (float(first) / float(second))

else:
    result = (float(first) * float(second))

print(result)

And when I choose 1, 2 or 3, it still multiplies. Anyone knows why?
I am a very inexperienced coder btw.
 
Solution
You need to convert the user's choice into an integer. Since the user's choice is a str, none of the if statements are fulfilled and it runs the code in the else statement. I have improved your code a bit as well.

Python:
operator = int(input("Choose an operator (1 = Add, 2 = Subtract, 3 = Divide, 4 = Multiply) "))
first = float(input("Enter an integer "))
second = float(input("Enter a second integer "))

if operator == 1:
    result = first + second

elif operator == 2:
    result = first - second

elif operator == 3:
    result = first / second

else:
    result = first * second

print(result)
You need to convert the user's choice into an integer. Since the user's choice is a str, none of the if statements are fulfilled and it runs the code in the else statement. I have improved your code a bit as well.

Python:
operator = int(input("Choose an operator (1 = Add, 2 = Subtract, 3 = Divide, 4 = Multiply) "))
first = float(input("Enter an integer "))
second = float(input("Enter a second integer "))

if operator == 1:
    result = first + second

elif operator == 2:
    result = first - second

elif operator == 3:
    result = first / second

else:
    result = first * second

print(result)
 
Solution
didlly is correct but at the same time, you have caused a new issues mate 😀 By changing the input to a int you now have the issue of, what if someone does input a string. What I would do is this :-

Python:
operator = input("Choose an operator (1 = Add, 2 = Subtract, 3 = Divide, 4 = Multiply) ")
first = 0
second = 0

while True:
    try:
        first = float(input("Enter an integer "))
        break
    except:
        print("First must be a digit and not a letter")
        continue

while True:
    try:
        second = float(input("Enter a second integer "))
        break
    except:
        print("Second must be a digit and not a letter")
        continue

if operator == "1":
    result = first + second

elif operator == "2":
    result = first - second

elif operator == "3":
    result = first / second

else:
    result = first * second

print(result)

So with my code, we do not change the string to int but instead, we check a string against a string, this stops one of the errors. then we do a loop of first and second. we use a try and except and when we have a int or float we move on 😀
 
You'll need to turn the user's selection into an integer. Because the user's selection is a str, none of the if statements are true, and the code in the otherwise statement is executed. I've also made some changes to your code.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom