Posts: 3
Threads: 2
Joined: Feb 2017
I am a newbie to python and using python 3.
I wish to learn patterns and printing e.g stars etc. I want to start basic patterns first
eg.
*
**
***
****
Is there a website or document like pdf that will teach me this and perhaps answers to check against when i go wrong?
Posts: 1,150
Threads: 42
Joined: Sep 2016
Hello,
I am not familiar with any document (nor did I search, really..). But if you look into "ASCII art" you might find a lot of resources for what you are after.
As far as Python goes, "print" statement will be your best friend in drawing patterns.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Feb-21-2017, 08:39 AM
(This post was last modified: Feb-21-2017, 08:39 AM by wavic.)
Simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
In [ 14 ]: for i in range ( 1 , 30 , 2 ):
...: print ( '*' * i)
...:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * *
|
1 2 3 |
In [ 15 ]: print ( 'oh, yes! ' * 10 )
oh, yes! oh, yes! oh, yes! oh, yes! oh, yes! oh, yes! oh, yes! oh, yes! oh, yes! oh, yes!
|
Posts: 4,220
Threads: 97
Joined: Sep 2016
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
numbers = { '***' : 128 , '** ' : 64 , '* *' : 32 , '* ' : 16 , ' **' : 8 , ' * ' : 4 , ' *' : 2 , ' ' : 1 }
def eca(rule, n = 18 , start = '*' ):
rule_bin = reversed ( bin (rule)[ 2 :])
last = start
for generation in range (n):
print (last)
last = ' {} ' . format (last)
next = ''
for index in range ( len (last) - 2 ):
if rule & numbers[last[index:index + 3 ]]:
next + = '*'
else :
next + = ' '
last = next
eca( 18 )
|
Posts: 2,953
Threads: 48
Joined: Sep 2016
Posts: 4,220
Threads: 97
Joined: Sep 2016
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import random
numbers = { '***' : 128 , '** ' : 64 , '* *' : 32 , '* ' : 16 , ' **' : 8 , ' * ' : 4 , ' *' : 2 , ' ' : 1 }
def box(rule, n = 23 , width = 79 ):
last = ''
for cell in range (width):
if random.random() < 0.5 :
last + = '*'
else :
last + = ' '
for generation in range (n):
print (last)
last = ' {} ' . format (last)
next = ''
for index in range ( len (last) - 2 ):
if rule & numbers[last[index:index + 3 ]]:
next + = '*'
else :
next + = ' '
last = next
box( 30 )
|
|