-1

I was making a basic addition calculator and here is what that code looked like:

Number_1 = input("what number?")
Operator = "will be added"
print(Operator)
Number_2 = input("What is the next number?")
A = Number_1+Number_2
print(A)  

The problem is if I put in 2 and 2 expecting to get 4, I get 22. I know what it is doing, joining the two numbers, but I don't really know how to fix this...

Psytho
  • 3,313
  • 2
  • 19
  • 27
  • 1
    Does this answer your question? [Why does input() always return a string?](https://stackoverflow.com/questions/43415978/why-does-input-always-return-a-string) – Psytho Feb 13 '23 at 09:04

2 Answers2

1

input returns a string, and adding two strings concatenates them. You need to cast the string to an integer using int.

Number_1 = int(input("what number?"))
Operator = "will be added"
print(Operator)
Number_2 = int(input("What is the next number?"))
A = Number_1+Number_2
print(A) 
Henry
  • 3,472
  • 2
  • 12
  • 36
1

input() always returns a string; you're going to need numbers if you want to add them up as numbers.

Assuming you'll want to go with integers to begin with (i.e. no decimals for now), use int() to cast those strings to numbers:

A = int(Number_1) + int(Number_2)

You could do that while input()ing too, á la

Number_1 = int(input("what number?"))

which might be clearer, as then you know you're always working with integers when the name of the variable is Number...

AKX
  • 152,115
  • 15
  • 115
  • 172