CAS Community   >   Resources   >  

Pythonprogramming
Python Program to print out a star triangle of any size

This challenge was set for my son during his recent interview for an IT job position which required some basic python skills

Mustapha Khoudi

Created by Mustapha Khoudi
last edited Jul 14 2021 by Mustapha Khoudi



Python program that will print out a star triangle/pyramid of any base of stars

how does it work?

The program is run as follows:

The program ask the user to enter the base which is an integer number of stars at the bottom of the triangle/pyramid. Then the program prints out the triangle

I am proposing two ways of doing it, my son’s way (his answer at the interview) and my way. Of course there are many other ways (more or less efficient!)

  • My Son’s Solution

    • Use of a basic loop: for each line of the triangle print the appropriate number of spaces and stars
      • Need to work out the number of lines that need to be looped over
  • My solution

    • I used this to introduce to the learners the use of the Zip built in function which zipps two iterables one for spaces and the other for stars

As always, the programs are commented to explain their logics

Examples

Alt text

Hope this helps!

Downloaded 158 times.

Download

This resource has attached files: to access these files, please tick the box below to assent to the license terms
License: The resources on CAS website are under Creative Commons Attribution-Share Alike 3.0 licence unless otherwise specified by the resource creators.

You must confirm that you have read and agree the licence's ToS before you can download the attachments of this resource.

I have read the licence agreement of this resource and agree to abide by its terms and conditions.

Feedback and Comments


Available when logged in (join via the front page, for free):
  • View 0 comments on this resource.
  • View resource history, links to related resources.
  • Leave feedback for the author(s), or help by editing the resource.
Categories:


© 2021 BCS, The Chartered Institute for IT Registered charity: No. 292786
Using the websiteDisclaimer of liabilityCookies policyPrivacy notice