[Tutor] Python Variable Addition

Dave Angel d at davea.name
Mon Apr 30 12:10:13 CEST 2012


On 04/30/2012 05:50 AM, viral shah wrote:
> Hi
>
> I'm new in the learning of python
>
> I want to make simple code for two variable addition
>
> Please help me in the following code
>
> *x = 12
> print x
> y =20
> print y
> z = x+y
> print 'Addition of above two numbers are : ' + int.z
> *
> when I run this same I got an error message :
>
> *AttributeError: type object 'int' has no attribute 'z'
>
> Please help me to solve the same error
>
>

Welcome to Python tutor.

Normally, the print statement accepts a list of items, separated by
commas.  Each item is implicitly converted to a string, and the results
are sent to standard out, with a space between each item.

So the most straightforward way to print z would be something like:

    print 'Addition of above two numbers are:',  z

Now, sometimes you want to combine two strings yourself, so you might use:

    print 'Addition of above two numbers are:' + str(z)

Here we are using the str type as a conversion function.  Note that we
used parentheses, not the dot operator.  And notice that since print
only gets one item, it does not add a space.





-- 

DaveA



More information about the Tutor mailing list