![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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 10:43 PM. Reason: mistake |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
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 Last edited by Wizard1988; Jul 6th, 2007 at 11:33 PM. |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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?
|
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
I think it just signifies that you will be listing variables next.
|
|
|
|
|
|
#8 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The quoted material below is from here.
Quote:
Quote:
__________________
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 |
||
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
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 2:34 PM. Reason: sample code added |
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Beginers Python Tutorial | Beegie_B | Python | 15 | Jul 28th, 2006 12:43 PM |
| [tutorial] Python for programming beginners | coldDeath | Python | 30 | Dec 14th, 2005 12:35 PM |
| Convert Python script to C++ code | clanotheduck | Python | 17 | Sep 25th, 2005 9:55 AM |
| Advanced Python Tricks | Arevos | Python | 19 | Sep 24th, 2005 8:39 AM |
| Python - A Programmers Introduction | coldDeath | Python | 17 | Aug 19th, 2005 1:41 PM |