Basic addition with Python, numbers don't add

Question:

I’m pretty new to Python and I was trying to make a basic addition program. Here is the source so far:

from os import system
import time

while True:
    system("cls")
    print "Number 1:"
    num1 = raw_input()
    system("cls")
    print "Number 2:"
    num2 = raw_input()
    system("cls")
    sum = num1 + num2
    print sum
    time.sleep(4)

It just puts num1 and num2 together instead of actually adding the numbers. Like if I put 4 + 4 it’d do 44 instead of 8. I understand WHY it does this I just want to know how to fix it.

Asked By: Prince

||

Answers:

You are summing strings, which results in concatenation, while you want to treat the values as numbers instead. Convert the string to a number first.

Use the int() function to convert to integer numbers, for example:

num1 = int(raw_input())
# ...
num2 = int(raw_input())
Answered By: Martijn Pieters
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.