[Tutor] re division problem

Kent Johnson kent37 at tds.net
Tue Feb 3 14:35:29 CET 2009


On Tue, Feb 3, 2009 at 8:24 AM, prasad rao <prasadaraon50 at gmail.com> wrote:
>> I wrote a function named vertical to print string .printable characters
>> and
> .>> ASCII values in a table.
>>> 1)It is swallowing some characters.
>>> 2)It output some characters  2 or 3 times.
>>> 3)It prints one column more than what I asked for.
>
>>> def vertical(columns):
>>>         import string
>>>         v=string.printable
>>>         v=v.replace('\n','')
>>> v=v.replace('\t','')
>
>>What is len(v) here? What is len(v)/columns?
> There are 100 characters  in the string(string.printable)
> I deleted '\n\t'. So 98/say 4==24.5 So if there is a fraction
> I have to round it off to 1. So 24.5 should be rounded off
> to 25..But is there a better way to print it  in given number
> of columns

Right. One way to do integer division that rounds up is like this:
skip = (len(v) + columns - 1) / columns

>
>>>         for x in range((len(v))/columns):
>>>                 for y in range(x,len(v),x+((len(v))/columns)):
>
>>The third argument to range() is the step size. Do you want to use a
>>different step size for each row?
> No. How can I ensure that every value is an integer  but without missing
> in retrieving an element in the string.

Not sure what you mean by that. How many elements do you need to skip
to get to the next column? Right now you skip by x+((len(v))/columns)
which will be different for each row.

Kent


More information about the Tutor mailing list