1

I'm beginning to learn coding on vscode with python and my code is this and it stacks the numbers.

num1 = input("Enter the first number:")

num2 = input("Enter the second number:")

sum = num1 + num2

print(sum)

and my terminal is

Enter the first number:10

Enter the second number:10
1010
PS C:\Users\Big Chungus\Desktop\python>
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
Alex Greer
  • 11
  • 1

2 Answers2

0

When you take an input from the user, it takes as a string.

To check the type, do this..

print(type(num1))
print(type(num2))

you will get

<class 'str'>
<class 'str'>

Strings cant be added but can be joined, thats why your answer was 1010.

If you want to add then numbers taken from the user, then you will need an int...

num1 = ("Enter the first number: ")
num2 = ("Enter the second number: ")

sum = int(num1)+int(num2)
print(sum)
Jiya
  • 745
  • 8
  • 19
-1

Require an integer input.

num1 = int(input("Enter the first number:"))
num2 = int(input("Enter the second number:"))

sum = num1 + num2

print(sum)

You can't add strings together and get the same result as integers.

Johnny
  • 211
  • 3
  • 15