PDA

View Full Version : optimising silly python table program



ingo
November 10th, 2008, 10:37 AM
Hi,

complete newbie to programming fighting my way through "Thinking like a python programmer". Below is a program to draw a table.

I am not pleased with the last three lines of the main function:
tableh(a)
tableh(a)
column(a)

Any ideas of how to optimise this?


def table(a):
def column(a):
print '+','- '*a+'+','- '*a+'+'
return
def row(a):
print ' |',' '*a*2,'|',' '*a*2,'|'
return
def tableh(a):
for b in range(a):
if b==0:
column(a)
else:
row(a)
return
tableh(a)
tableh(a)
column(a)
return
a=input("gissa a number then: ")
table(a)Many thanks in advance for any hints/suggestions/ideas :)

geirha
November 10th, 2008, 10:58 AM
How about something like this?

for i in range(a*2):
if i % a == 0: column(a)
else: row(a)
column(a)


Btw, you should use raw_input instead of input. input is not safe.

ingo
November 10th, 2008, 11:03 AM
Wow, that is stuff I haven't come across yet, in particular % - I'll be on the lookout for that one :)

Reason I used input was to get an integer, raw_input produces a type error.

elbarto_87
November 10th, 2008, 11:08 AM
Wow, that is stuff I haven't come across yet, in particular % - I'll be on the lookout for that one :)

Reason I used input was to get an integer, raw_input produces a type error.

You can use the int() function to convert raw_input to an integer



a = raw_input("Enter a number:")
a = int(a)

Obviously that piece of code is pretty trivial but it might help you out in something else....

geirha
November 10th, 2008, 11:09 AM
Also, to make it even shorter, instead of using functions to print one line, just store column and row in a string, then print that string later



def table(a):
column='+ '+'- '*a+'+ '+'- '*a+'+'
row='| '+' '*a*2+'| '+' '*a*2+'|'
for i in range(a*2): print i % a == 0 and column or row
print column


The % operator gives you the remainder in integer division. E.g. 5 divided by 3 is 1 with a remainder of 2, so

>>> 5/3
1
>>> 5%3
2


To use raw_input to get an int, do something like:

a=raw_input("gissa a number then: ")
try:
a= int(a)
table(a)
except ValueError:
print "Not a valid integer"

ingo
November 10th, 2008, 11:24 AM
Thank you very much, that has shrunk the program quite a bit :) Why I didn't think that column and row could be simple strings I'll never know :D

ingo
November 10th, 2008, 11:45 AM
Actually, I'm still chewing over this line


for i in range(a*2): print i % a == 0 and column or rowIf I understand it correctly, it works like this:

If the value after the print command is 0, the "and" causes a column to be printed while other values result in rows

I have never come across that notation - cool!

geirha
November 10th, 2008, 11:54 AM
Yes, be a bit careful when using it though. For instance, empty sequences and empty strings are considered False. http://diveintopython.org/power_of_introspection/and_or.html