Domanda

Qualcuno può dirmi perché num_caratt e num_rows hanno la stessa?

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

L'uscita

.....|*****|*****|*****|*****|*****|*****|*****|
.....|.....|*****|*****|*****|*****|*****|*****|
.....|.....|.....|*****|*****|*****|*****|*****|
.....|.....|.....|.....|*****|*****|*****|*****|
.....|.....|.....|.....|.....|*****|*****|*****|
.....|.....|.....|.....|.....|.....|*****|*****|
.....|.....|.....|.....|.....|.....|.....|*****|
.....|.....|.....|.....|.....|.....|.....|.....|

E ora di cambiare num_caratt a 15.

.....|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
.....|*****|*****|*****|*****|*****|*****|*****|
È stato utile?

Soluzione

Hai detto che stai usando ctypes, perché si vuole mutevole char buffer per questo.Ma è possibile ottenere l'output desiderato dall'elenco comprensione

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)
)

.....|*****|*****|*****|*****|*****|*****|*****
.....|.....|*****|*****|*****|*****|*****|*****
.....|.....|.....|*****|*****|*****|*****|*****
.....|.....|.....|.....|*****|*****|*****|*****
.....|.....|.....|.....|.....|*****|*****|*****
.....|.....|.....|.....|.....|.....|*****|*****
.....|.....|.....|.....|.....|.....|.....|*****
.....|.....|.....|.....|.....|.....|.....|.....

MODIFICA

Vi mostrerò come è possibile utilizzare list comprehensions per disegnare qualsiasi cosa char bitmap che si desidera disegnare.L'idea è semplice.Costruire una matrice booleana con Vera in luoghi che si desidera stampare il carattere e False in caso contrario.E basta usare la 'o' di trucco per stampare il carattere giusto.Questo esempio per costruire un gioco, come consiglio.È possibile utilizzare lo stesso concetto di disegnare qualsiasi forma che si desidera.

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)
)

Altri suggerimenti

Ci siamo.Ho avuto riganum_rows invece di filanum_caratt ho necessario bisogno di una Dr Pepper.E comunque, non era questo il compito.Per un LCD progetto.

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top