Sunday, 15 August 2010

python - Formatted Printing:Boolean Issue -



python - Formatted Printing:Boolean Issue -

the purpose of piece of code take in string , print out in specific format. instance, given:

s = "hello"

the programme should print out:

+---+---+---+---+---+ | h | e | l | l | o | +---+---+---+---+---+

if size of string greater column size of console, it's supposed print out string in format:

+---+---+---+---+---+ | h | e | l | l | o | +---+---+---+---+---+ | h | e | l | l | o | +---+---+---+---+---+ | h | e | l | l | o | +---+---+---+---+---+ | h | e | l | l | o | +---+---+---+---+---+

unfortunately, sec status doesn't work , can't seem figure out why.

here's code:

import os s = "hello"*20 (consolerows,consolecol)=os.popen('stty size','r').read().split() top = outer = "+---"*len(s)+'+'+'\n' in range(len(s)): outer += "| "+s[i]+" " outer += '|\n' outer += top[:len(top)-1] split = outer.split('\n') if(len(split[0]) > consolecol): #problem lies on line. though size of outer = outer.split('\n') #split[0] greater consolecol if statement beg = 0 #isn't entered. size = consolecol print(outer[0][beg:size]) while(size < len(outer[0])): print(outer[1][beg:size]); print(outer[2][beg:size]); beg = size size += size else: print(outer)

can see problem is? printed out size of outer[0] , of consolecol. len(output[0]) greater consolecol.

bug in code: consolecol string, not integer.

replace (consolerows,consolecol)=os.popen('stty size','r').read().split() (consolerows,consolecol)=map(int, os.popen('stty size','r').read().split())

this still doesn't produce desired result - you'll have figure rest out , maybe clean code in process.

python

No comments:

Post a Comment