Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Jul 6th, 2007, 10:10 PM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 416
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
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
__________________
JG-Webdesign

Last edited by Wizard1988; Jul 6th, 2007 at 10:33 PM.
Wizard1988 is offline   Reply With Quote
Old Jul 6th, 2007, 10:23 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 6th, 2007, 10:52 PM   #4
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.
357mag is offline   Reply With Quote
Old Jul 6th, 2007, 10:55 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 6th, 2007, 11:40 PM   #6
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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?
357mag is offline   Reply With Quote
Old Jul 7th, 2007, 12:01 AM   #7
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 416
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
I think it just signifies that you will be listing variables next.
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Jul 7th, 2007, 8:40 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.)
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 10th, 2007, 1:12 PM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Cool

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
"""
__________________
I looked it up on the Intergnats!

Last edited by Dietrich; Jul 10th, 2007 at 1:34 PM. Reason: sample code added
Dietrich is offline   Reply With Quote
Old Jul 10th, 2007, 1:15 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There's a link in the post; perhaps you should inform them .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginers Python Tutorial Beegie_B Python 15 Jul 28th, 2006 11:43 AM
[tutorial] Python for programming beginners coldDeath Python 30 Dec 14th, 2005 11:35 AM
Convert Python script to C++ code clanotheduck Python 17 Sep 25th, 2005 8:55 AM
Advanced Python Tricks Arevos Python 19 Sep 24th, 2005 7:39 AM
Python - A Programmers Introduction coldDeath Python 17 Aug 19th, 2005 12:41 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:43 PM.

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