Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   How would I do this in Python? (http://www.programmingforums.org/showthread.php?t=13491)

357mag Jul 6th, 2007 10:42 PM

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.

Wizard1988 Jul 6th, 2007 11:10 PM

I don't usually play with python but here is something you can try.

:

def square(x):
    return x * x

print "Value\tSquare"

for i in range(1,6):
    print '%1d \t\t %2d' % (i, square(i))


Or:

:

print "Value\tSquare"

for i in range(1,6):
    print '%1d\t\t%2d' % (i,i*i)


Yet better:

:

print "Value\tSquare"

for i in range(1,6):
    print '%5d %8d' % (i,i*i)


You might want to read this about formatting strings

DaWei Jul 6th, 2007 11:23 PM

The problem with tabs, as you might or might not have discovered in your other thread, is that they represent "tab stops". If you're not familiar with typewriters, this might mean nothing. In many, many implementations they are considered to be set at multiples of 8. People come along and dink with that number all the time.

This is fine if every number you are ever going to output is smaller than the unknown size you have to deal with. Otherwise, you've been effed without being kissed.

If you want control over the appearance of your output, you need to look to the output formatting provisions of each language you fart with. Programmers have been dealing with this since they were in diapers. Some numbers need a space for a sign, even if it's blank. Some need trailing space for possible exponential notation. Some need to be right-justified. Some need to be left justified. Some need to be aligned on the decimal point, regardless of the length to either side.

The writers of the languages considered this, but they failed to imbue the language with enough magic to read your mind. You're supposed to be the one in control. Not every language is well documented, but you at least should look before you leap.

357mag Jul 6th, 2007 11:52 PM

I adjusted the numbers a bit:

:

print '%3d  %7d' % (i, square(i))

The numbers 3 and 7 that follow the percentage sign I believe mean to tell the compiler to right justify the number in a field width of 3 character postions, and the next number would be right justified in a field width of 7 character positions. But when I count the number of character positions from the first number to where the second number(under the Square column)is located, no way is that just 7. It's gotta be more than that.

DaWei Jul 6th, 2007 11:55 PM

Spaces that you put in the format string are put directly onto the output. Again, I'd recommend you consult the documentation. That can be confusing when you're trying to learn three languages in three days, I'll admit.

357mag Jul 7th, 2007 12:40 AM

Another question. The percentage sign. My understanding is that the percentage sign is just a placeholder for a value. So the first value of i would go into the %3d position, and the squared value of i would go into the %7d position. But what is the last percentage sign doing? The percentage sign right before the (i, square(i)) part?

Wizard1988 Jul 7th, 2007 1:01 AM

I think it just signifies that you will be listing variables next.

DaWei Jul 7th, 2007 9:40 AM

The quoted material below is from here.
Quote:

The % operator interprets the left argument much like a sprintf()-style format string to be applied to the right argument, and returns the string resulting from this formatting operation.
Quote:

:

>>> for x in range(1,11):
...    print '%2d %3d %4d' % (x, x*x, x*x*x)
...
 1  1    1
 2  4    8
 3  9  27
 4  16  64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000

(Note that one space between each column was added by the way print works: it always adds spaces between its arguments.)

Dietrich Jul 10th, 2007 2:12 PM

Quote:

(Note that one space between each column was added by the way print works: it always adds spaces between its arguments.)
Whoever wrote that is full of dung, as it pertains to formatted strings. The space that you have in this example is the one from the format string.

Look at this:
:

for i in range(1, 6):
    print "%d-->%d" % (i, i*i)
"""
result (no spaces) ...
1-->1
2-->4
3-->9
4-->16
5-->25
"""


DaWei Jul 10th, 2007 2:15 PM

There's a link in the post; perhaps you should inform them ;).


All times are GMT -5. The time now is 2:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC