Need Help in Python

  • Thread starter Robert M. Emmons
  • Start date
R

Robert M. Emmons

Mahmood said:
Hello everyone. I started programming in Python like a month ago and i
have a problem. I want the program that I am writing to use a
character to draw a box. How can i declare that character as a
variable? If anyone could help me. Thanks

Python has no declaration. Character strings are like any other variable:

x = "a"

or

x = "abcdef"

or

x = chr(35)

Several options.

Rob
 
M

Mahmood Mashal

Hello everyone. I started programming in Python like a month ago and i
have a problem. I want the program that I am writing to use a
character to draw a box. How can i declare that character as a
variable? If anyone could help me. Thanks
 
L

Larry Bates

Line drawing characters can be used:

topleftbox=chr(218)
bottomleftbox=chr(192)
bottomrightbox=chr(217)
toprightbox=chr(191)
vertical=chr(179)
horizontal=chr(196)

HTH,
Larry Bates
Syscon, Inc.
 
C

Cousin Stanley

Larry ....

Thanks for the reminder to use line drawing characters ....

Example Box follows ....

'''
NewsGroup .... comp.lang.python
Date ......... 2004-05-05
Posted_By .... Larry Bates
Edited_By .... Stanley C. Kitching
'''

# Line drawing characters ....

box_top_left = chr( 218 )
box_bot_left = chr( 192 )
box_bot_right = chr( 217 )
box_top_right = chr( 191 )

vertical = chr( 179 )
horizontal = chr( 196 )


NL = '\n'
fill = ' ' * 8
h_42 = horizontal * 42
SP_42 = ' ' * 42


top = fill + box_top_left + h_42 + box_top_right

sides = ( fill + vertical + SP_42 + vertical + NL ) * 21

sides = sides[ : -1 ]

bot = fill + box_bot_left + h_42 + box_bot_right + NL

print
print top
print sides
print bot
 
D

Daniel 'Dang' Griffith

""" Starter module for drawing boxes with characters.
No warranty or suitability for use is expressed or implied.
Daniel 'Dang' Griffith sez, "Have a nice day!".
"""

# for reference
boxchars = [chr(i) for i in xrange(129, 219)]

# handy abbreviations; roll your own naming convention
UL = upperleft = topleft = chr(218)
LL = lowerleft = botleft = bottomleft = chr(192)
LR = lowerright = botright = bottomright = chr(217)
UR = upperright = topright = chr(191)
vert = vertical = chr(179)
horiz = hor = horizontal = chr(196)

nl = newline = '\n'

# precompute the invariants
top = '%c%%s%c' % (UL, UR)
mid = '%%s%c%%s%c\n' % (vert, vert)
bot = '%c%%s%c' % (LL, LR)
all = '%s\n%%s%%s%s' % (top, bot)

def box(w, h, fill=' ', indent=''):
""" Return a string that appears as an 'box' when printed.
This is, of course, highly depending on the local
character representation.
The box is filled with spaces by default.
The box is left-aligned by default. To indent it, provide
the indentation string. This will be added to the middle
and bottom of the box. The caller is responsible for
drawing the box at the appropriate starting position.
"""
# exceptional cases
if w < 1:
raise ValueError("A box must have a width of at least 1.")
if h < 1:
raise ValueError("A box must have a height of at least 1.")
# pathological cases--return value is not *really* a box
if w == 1:
return h * (vert + nl)
if h == 1:
return horiz * w
# normal case
end = horiz * (w - 2)
return all % (end, (h - 2) * (mid % (indent, fill * (w - 2))),
indent, end)

# --dang
 
C

Cousin Stanley

""" Starter module for drawing boxes with characters.
No warranty or suitability for use is expressed or implied.
Daniel 'Dang' Griffith sez, "Have a nice day!".
"""

Daniel ....

Thanks for a nice generalization ....

Preliminary tests indicate a problem
with indent strings NOT being applied to the FIRST, top, line
in the returned string ....

I'll try to find the problem ....
 
D

Daniel 'Dang' Griffith

Daniel ....

Thanks for a nice generalization ....

Preliminary tests indicate a problem
with indent strings NOT being applied to the FIRST, top, line
in the returned string ....

I'll try to find the problem ....
That was intentional, as mentioned in the comments (perhaps
poorly). The caller is responsible for positioning the
cursor at the top left corner, which means the first line
should not be indented. This allows things like this (excuse
my poor ASCII art manual
example:/---\
| |
| |
| |
\---/

Nonetheless, it's Friday. Maybe this should go to the Useless Python
site. http://www.uselesspython.com/oldindex.html (Did anyone check?
Maybe there's something similar already there!) Here's an enhanced
version that a) optionally prefixes the top line (the caller passes
True after the specified indentation string), and b) fixes a bug if
the fill value was longer than 1 character.
--dang
""" Starter module for drawing boxes with characters.
No warranty or suitability for use is expressed or implied.
Daniel 'Dang' Griffith sez, "Have a nice day!".
"""

# for reference
boxchars = [chr(i) for i in xrange(129, 219)]

# handy abbreviations; roll your own naming convention
UL = upperleft = topleft = chr(218)
LL = lowerleft = botleft = bottomleft = chr(192)
LR = lowerright = botright = bottomright = chr(217)
UR = upperright = topright = chr(191)
vert = vertical = chr(179)
horiz = hor = horizontal = chr(196)

nl = newline = '\n'

# precompute the invariants
top = '%%s%c%%s%c' % (UL, UR)
mid = '%%s%c%%s%c\n' % (vert, vert)
bot = '%c%%s%c' % (LL, LR)
all = '%s\n%%s%%s%s' % (top, bot)

def box(w, h, fill=' ', indent='', top_also=False):
""" Return a string that appears as an 'box' when printed.
This is, of course, highly depending on the local
character representation.
The box is filled with spaces by default.
The box is left-aligned by default. To indent it, provide
the indentation string. This will be added to the middle
and bottom of the box.
By default, the caller is responsible for drawing the box
at the appropriate starting position. To have to top
line of the box indented, pass True as the last agrument.
"""
# exceptional cases
if w < 1:
raise ValueError("A box must have a width of at least 1.")
if h < 1:
raise ValueError("A box must have a height of at least 1.")
# pathological cases--return value is not *really* a box
if w == 1:
return h * (vert + nl)
if h == 1:
return horiz * w
# normal case
if not fill:
fill = ' '
fill = (fill * (w - 2))[:w-2]
end = horiz * (w - 2)
return all % (top_also and indent or '', end, (h - 2) * (mid %
(indent, fill)), indent, end)
# --dang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top