Python Forum
printing patterns and stars?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing patterns and stars?
#1
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?
Reply
#2
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.
Reply
#3
Simple example:

In [14]: for i in range(1, 30, 2):
    ...:     print('*' * i)
    ...:     
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
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! 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
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)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Think
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
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)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help me to find turtle codes for those patterns hammza 0 1,412 May-12-2020, 12:14 PM
Last Post: hammza

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020