0

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
Oliveira
  • 3
  • 2
  • `input()` returns strings (assuming you're using Python 3). When you add strings in Python you get concatenated strings. `"fred" + "bob"` returns `"fredbob"`. `"1" + "2"` returns `"12"`. – Steven Rumbalski Oct 23 '22 at 03:02
  • 1
    `R == ...` looks like a typo, double `=` for comparison and single `=` for assignment. – Mechanic Pig Oct 23 '22 at 03:08

2 Answers2

0

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: "))
Elijah Nicol
  • 141
  • 7
-1

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.

Darryl Johnson
  • 646
  • 6
  • 14