I am new here and it's my first post, so I'm sorry if this is in the wrong place or is formatted wrong. I made my first Python
code, which is extremely simple, but I have a few questions I need clarified:

This program asks the user to enter 3 integers, and will add them.
num1 = int(raw_input("Enter an integer"))
num2 = int(raw_input("Enter a second integer"))
num3 = int(raw_input("Enter a third integer"))
ans = num1 + num2 + num3
print "The sum of the 3 integers entered is", ans

I'm trying to make sure I fully understand the basics.  Whenever raw_input is used, the program sees the inputted value as a string, so I made sure to convert them to integers.  I understand this much. On the line that says "ans = num1 + num2 + num3", is this "ans" variable considered to be an integer, since the num1, num2, and num3 variables were?
My main question involves the last line.  I know that the part of the line that says "The sum of the 3 integers entered is" is a string.  is the ", ans" part at the end still an integer?  Also, is there another way I could have written this last line, using a plus sign?  I feel like I remember seeing a program where the print statement put the variables in using plus signs or something...
I'm sorry if these are silly questions, but programming does not come naturally to me, so I want to be sure I am thinking about this correctly.  Thank you.

Hi frankie198, welcome to DaniWeb!
There are no silly questions. Only dumb answers; like mine perhaps. :)
As I'm a learner of Python myself, the Python interprter can infer what type is at hand and acts acordingly. So if you print an integer type it will be auto converted to a string. The great Python gurus around here will probably tell you more.

Edited 2 Years Ago by ddanbe: addition

You have convert all input to integers,so answer(ans) is also an integer.
Can look at some improvement.

num1 = int(raw_input("Enter an integer"))
num2 = int(raw_input("Enter a second integer"))
num3 = int(raw_input("Enter a third integer"))
print "The sum of the 3 integers entered is: {}".format(sum((num1, num2, num3)))

So in this version use build in sum() and string formatting.

>>> a = 1, 2, 3
>>> a
(1, 2, 3)
>>> sum(a)
6
>>> print '{} {}'.format('my score is' , sum(a))
my score is 6

If you think of it,it's really the same answer repeating 3 times.

result = []
for times in range(3):
    result.append(int(raw_input("Enter an integer: ")))
print "The sum of the 3 integers entered is: {}".format(sum(result))

Edited 2 Years Ago by snippsat

Comments
Super!

In short:

I'm trying to make sure I fully understand the basics. Whenever raw_input is used, the program sees the inputted value as a string, so I made sure to convert them to integers. I understand this much. On the line that says "ans = num1 + num2 + num3", is this "ans" variable considered to be an integer, since the num1, num2, and num3 variables were?

Correct. ans is an integer!

My main question involves the last line. I know that the part of the line that says "The sum of the 3 integers entered is" is a string. is the ", ans" part at the end still an integer?

ans will always be an integer until it is modified!

Also, is there another way I could have written this last line, using a plus sign? I feel like I remember seeing a program where the print statement put the variables in using plus signs or something...

There are a few ways:
(1) You can just replace ans with (a + b + c). For example:

a = int(raw_input("Enter first integer: "))
b = int(raw_input("Enter second integer: "))
c = int(raw_input("Enter third integer: "))
print "The sum is:", (a + b + c)

(2) You can find the sum, convert it into a string, and append it to what's being printed out. Ie: print "The sum is: " + str(a + b + c).

(3) You can keep it just the way it is. There's no advantage to doing math in the same line you're printing with.

Edited 2 Years Ago by Hiroshe

Actually using string formatting would be the most pythonic way:

num1 = int(raw_input("Enter an integer"))
num2 = int(raw_input("Enter a second integer"))
num3 = int(raw_input("Enter a third integer"))
ans = num1 + num2 + num3
print("The sum of the 3 integers entered is {}".format(ans))

You might also like to see this Python 3... example:
(that uses a function to loop until valid integer input)
(and that loops to ask user if more numbers to be input)
(and that demo's the use of a list 'generator')
(and that demo's the use of 'join')

# sumNumbers.py #

def takeInInt( msg ):
    loop = True
    while( loop ):
        try:
            i = int(input(msg))
            loop = False
        except( ValueError ):
            print( "Only integers valid here ... try again ..." )
    return i


nums = []
more = True
while( more ):
    num = takeInInt( "Enter next integer to sum: " )
    nums.append( num )
    more = ( input( "More (y/n) ?" ).upper() != 'N' )

lstNumAsStrsGen = list( str(x) for x in nums )
print( '+'.join(lstNumAsStrsGen), '=', sum(nums) )

Edited 2 Years Ago by David W: adjusted word order

This question has already been answered. Start a new discussion instead.