how to do draw pattern with python?

E

echo.hping

may i know how to shift the bits using only looping and branching??

x....x
..x..x.
...xx..
...xx..
..x..x.
x....x

xx....
...x..x
....xx.
....xx.
...x..x
xx....

..xx...
x..x..
.....xx
.....xx
x..x..
..xx...

etc..
 
M

Mark Lawrence

may i know how to shift the bits using only looping and branching??

x....x
.x..x.
..xx..
..xx..
.x..x.
x....x

xx....
..x..x
...xx.
...xx.
..x..x
xx....

.xx...
x..x..
....xx
....xx
x..x..
.xx...

etc..

You write some code and test it. If it doesn't work you cut and paste
the smallest sample of code that can reproduce the problem, together
with the full traceback if applicable, and you're likely to get plenty
of answers.
 
D

Dave Angel

may i know how to shift the bits using only looping and branching??

Yes, show us your code, and what isn't working, and we'll try to help
you complete the assignment. It'd probably also be good to specify the
rest of the homework, like what version of what language it has to be
implemented in.

I don't see any bits, only strings of characters. And it seems to me
that using slices is the most obvious mechanism for rotating
fixed-length strings.
 
P

Peter Otten

may i know how to shift the bits using only looping and branching??

import time

data = """\
x....x
..x..x.
...xx..
...xx..
..x..x.
x....x

""".splitlines()

data = [line * 12 for line in data] # optional

while True:
print "\x1b[2J\x1b[0;0H" # optional
for i, line in enumerate(data):
print line
data = line[1:] + line[:1]
time.sleep(.1)

Doing your homework since 2001 ;)
 
I

Ismael Farfán

2012/9/21 Peter Otten said:
(e-mail address removed) wrote:

print "\x1b[2J\x1b[0;0H" # optional

Nice code : )

Could you dissect that weird string for us?

It isn't returning the cursor to (0,0), it's just like executing
clear(1), and looks like those line coloring scape sequences for bash.

Ismael
 
P

Peter Otten

Ismael said:
2012/9/21 Peter Otten said:
(e-mail address removed) wrote:

print "\x1b[2J\x1b[0;0H" # optional

Nice code : )

Could you dissect that weird string for us?

It isn't returning the cursor to (0,0), it's just like executing
clear(1), and looks like those line coloring scape sequences for bash.

"\x1b[2J" or ESC [2J should clear the screen and

"\x1b[1;1H" or ESC [1;1H should move the cursor to the origin (I got that
wrong in the previous post)

There may be other problems -- I stopped reading

http://en.wikipedia.org/wiki/ANSI_escape_code

as soon as I got the desired effect (scrolling) in konsole.
 
C

Chris Angelico

2012/9/21 Peter Otten said:
(e-mail address removed) wrote:

print "\x1b[2J\x1b[0;0H" # optional

Nice code : )

Could you dissect that weird string for us?

It isn't returning the cursor to (0,0), it's just like executing
clear(1), and looks like those line coloring scape sequences for bash.

It's an ANSI escape sequence, or rather two of them. The first one
clears the screen, the second returns you to 0,0. (Isn't that implicit
in the 2J code? Maybe I'm misremembering.) But it depends on the
terminal responding to them, and not all terminals do. For instance,
most MUD clients parse only a very small subset of ANSI codes, eg
color codes only.

ChrisA
 
I

Ian Kelly

2012/9/21 Peter Otten said:
(e-mail address removed) wrote:

print "\x1b[2J\x1b[0;0H" # optional

Nice code : )

Could you dissect that weird string for us?

It isn't returning the cursor to (0,0), it's just like executing
clear(1), and looks like those line coloring scape sequences for bash.

They're called "ANSI escape codes". :)

CSI 2J clears the screen.
CSI 0;0H means "move the cursor to row 0, column 0". However, I don't
think that's valid ANSI, as the coordinates are 1-based. Probably it
should have been "\x1b[2J\x1b[1;1H".
 
C

Chris Angelico

It's an ANSI escape sequence, or rather two of them. The first one
clears the screen, the second returns you to 0,0. (Isn't that implicit
in the 2J code? Maybe I'm misremembering.)

Ah. From Wikipedia:
"If n is two, clear entire screen (and moves cursor to upper left on
MS-DOS ANSI.SYS)."

So adding \e[H is necessary.

ChrisA
 
M

Mark Lawrence

may i know how to shift the bits using only looping and branching??

import time

data = """\
x....x
.x..x.
..xx..
..xx..
.x..x.
x....x

""".splitlines()

data = [line * 12 for line in data] # optional

while True:
print "\x1b[2J\x1b[0;0H" # optional
for i, line in enumerate(data):
print line
data = line[1:] + line[:1]
time.sleep(.1)

Doing your homework since 2001 ;)


I tried running your code but got this:-

c:\Users\Mark>pattern.py
File "C:\Users\Mark\pattern.py", line 22
Doing your homework since 2001
^
SyntaxError: invalid syntax

What am I doing wrong?
 
H

Hans Mulder

2012/9/21 Peter Otten said:
(e-mail address removed) wrote:

print "\x1b[2J\x1b[0;0H" # optional

Nice code : )

Could you dissect that weird string for us?

It isn't returning the cursor to (0,0), it's just like executing
clear(1), and looks like those line coloring scape sequences for bash.

They're called "ANSI escape codes". :)

CSI 2J clears the screen.
CSI 0;0H means "move the cursor to row 0, column 0". However, I don't
think that's valid ANSI, as the coordinates are 1-based. Probably it
should have been "\x1b[2J\x1b[1;1H".

Yes, the coordinates are 1-base, so it should have been
"\x1b[2J\x1b[1;1H". Or, since 1;1 is the default, "\x1b[2J\x1b[H".

On my machine, clear(1) uses "\x1b[H\x1b[2J".

Using clear(1) appears to be the most portable way to do it:

import os, time

data = """\
x....x
..x..x.
...xx..
...xx..
..x..x.
x....x

""".splitlines()

data = [line * 12 for line in data] # optional

try:
while True:
os.system("clear") # optional
for i, line in enumerate(data):
print line
data = line[1:] + line[:1]
time.sleep(.1)
except KeyboardInterrupt:
pass



Hope this helps,

-- HansM
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top