Some help with some Python code
Can anyone tell me why num_chars and num_rows should be the same?
from ctypes import *
num_chars = 8
num_rows = 8
num_cols = 6
buffer = create_string_buffer (num_chars*num_rows*num_cols+num_chars)
for char in range(num_chars):
for row in range(num_rows):
for col in range(num_cols):
if char == num_chars-1 and col == num_cols-1:
buffer[row*num_rows*num_cols+char*num_cols+col+row] = '|'
buffer[row*num_rows*num_cols+char*num_cols+col+row+1] = '\n'
elif col == num_cols-1:
buffer[row*num_rows*num_cols+char*num_cols+col+row] = '|'
else:
buffer[row*num_rows*num_cols+char*num_cols+col+row] = ('.', '*')[char>row]
print buffer.value
Exit
.....|*****|*****|*****|*****|*****|*****|*****|
.....|.....|*****|*****|*****|*****|*****|*****|
.....|.....|.....|*****|*****|*****|*****|*****|
.....|.....|.....|.....|*****|*****|*****|*****|
.....|.....|.....|.....|.....|*****|*****|*****|
.....|.....|.....|.....|.....|.....|*****|*****|
.....|.....|.....|.....|.....|.....|.....|*****|
.....|.....|.....|.....|.....|.....|.....|.....|
And now change num_chars to 15.
.....|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
.....|*****|*****|*****|*****|*****|*****|*****|
-3
a source to share
2 answers
You said you were using ctypes because you want to use a mutable char buffer for that. But you can get the desired result from a list comprehension.
num_chars = 5
num_rows = 8
empty = ['.' * num_chars]
full = ['*' * num_chars]
print '\n'.join(
'|'.join(empty * (i + 1) + (num_rows - i - 1) * full)
for i in xrange(num_rows)
)
.....|*****|*****|*****|*****|*****|*****|*****
.....|.....|*****|*****|*****|*****|*****|*****
.....|.....|.....|*****|*****|*****|*****|*****
.....|.....|.....|.....|*****|*****|*****|*****
.....|.....|.....|.....|.....|*****|*****|*****
.....|.....|.....|.....|.....|.....|*****|*****
.....|.....|.....|.....|.....|.....|.....|*****
.....|.....|.....|.....|.....|.....|.....|.....
EDIT
I'll show you how you can use list methods to draw any char bitmaps you want to draw. The idea is simple. Create a boolean array with True where you want to print the character and False otherwise. And just use the "or" trick to print the correct character. In this example, a checkerboard will be built. You can use the same concept to draw any shape.
rows = 5
cols = 6
char = '#'
empty = '.'
bitmap = [[ (i + j)%2 == 0 for i in xrange(cols)] for j in xrange(rows)]
print '\n'.join(
'|'.join(bitmap[j][i] * char or empty for i in xrange(cols))
for j in xrange(rows)
)
+5
a source to share
There we go. I had rownum_rows instead of rownum_chars I needed Dr Pepper. And by the way, this was not homework. This is for an LCD project.
num_chars = 10
num_rows = 8
num_cols = 6
buffer = create_string_buffer (num_chars*num_rows*num_cols+num_chars)
for char in range(num_chars):
for row in range(num_rows):
for col in range(num_cols):
if char == num_chars-1 and col == num_cols-1:
buffer[row*num_chars*num_cols+char*num_cols+col+row] = '|'
buffer[row*num_chars*num_cols+char*num_cols+col+row+1] = '\n'
elif col == num_cols-1:
buffer[row*num_chars*num_cols+char*num_cols+col+row] = '|'
else:
buffer[row*num_chars*num_cols+char*num_cols+col+row] = ('.', '*')[char>row]
print repr(buffer.raw)
print buffer.value
+1
a source to share