View Single Post
Old Jul 6th, 2007, 9:42 PM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
How would I do this in Python?

I've written this program both in Java and in C++, so now I'm trying to do it in Python. All the program does is use a loop from 1 to 5 and output each number followed by that number squared. It outputs the numbers in columns, so it needs to look like this:

Value     Square
   1            1
   2            4
   3            9
   4            16
   5            25

Here is all I got so far:

def square(x):
    return x * x
    

print "Value   Square"


for i in range(1, 6):
    print i + "   " + square(i)

But of course it's wrong cuz the compiler tells me I can't concatanate and integer with " ". I'm looking for some way to tell the compiler to first print the value of i then move over to the next column, and print that value squared. Then after that, move to the next line and do the same for the next value of i.

Last edited by 357mag; Jul 6th, 2007 at 9:43 PM. Reason: mistake
357mag is offline   Reply With Quote