instead of summing the two values, it just repeats it

Question:

my code in python is supposed to sum the two variables and return a value, but it keeps returning the two numbers together:

A = input("insert a value: ")
B = input("insert another value: ")
if A >= B:
    R == A + B 
    print ("this is the result", R)
else:
    R == A - B
    print ("this is the result", R)


input 1: A=1 and B=1
output 1: R=11

input 2: A=2 and B=1
output 2: R=21
Asked By: Oliveira

||

Answers:

I don’t write python code, but you need to cast the input from type String to Int

A_INPUT = input("insert a value: ")
B_INPUT = input("insert another value: ")

A = int(A_INPUT)
B = int(B_INPUT)

if A >= B:
    R == A + B 
    print ("this is the result", R)
else:
    R == A - B
    print ("this is the result", R)

https://www.freecodecamp.org/news/python-convert-string-to-int-how-to-cast-a-string-in-python/#:~:text=To%20convert%2C%20or%20cast%2C%20a,int(%22str%22)%20.

Answered By: Darryl Johnson

input() returns a string (str) object, so when you try to add A and B together, they end up being concatenated.

You need to cast the result from input() to an integer (int). Try something like this instead:

A = int(input("insert a value: "))
B = int(input("insert another value: "))
Answered By: Elijah Nicol
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.