Python Forum
Making a number triangle that points to the left
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a number triangle that points to the left
#1
As part of an assignment, I am supposed to make a number triangle that points to the left using only for loops and string format. I can kind of get it working using nested for loops, but I can't figure out how I can implement it with string formatting.

This is what I have so far, but it is a huge difference from the output that I am supposed to be getting.
n = int(input("Level(1-15) ==> "))
for i in range(1, n):
   for j in range(n - i):
       print(" ", end = " ")
   for j in range(1, i):
       print(j, end = " ")
   print("\n")
Expected output if the user enters 5:

Output:
           1         2  3      4  5  6   7  8  9  10 11 12 13 14 15
I would appreciate if someone could help me since my code is nowhere near that. Also, the numbers in the pyramid should be exactly the same, another problem that I cannot figure out how to solve. Any help would be appreciated.
Reply
#2
https://docs.python.org/3.6/library/stri...i-language

Think of fill and width properties
Reply
#3
(Jul-12-2017, 07:29 PM)buran Wrote: https://docs.python.org/3.6/library/stri...i-language

Think of fill and width properties

Thanks for the link. I still do not understand how to get those numbers all lined up together though. All I got from that link was I have to use space as fill and set width to user input. Is that correct?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a recursion syntax for Sierpinski triangle using numpy? Bolzano 2 3,805 Apr-03-2021, 06:11 AM
Last Post: SheeppOSU
  Evaluating math from left to right in coding hannahlynn 3 2,414 Mar-23-2021, 09:31 PM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,343 Nov-07-2020, 07:01 PM
Last Post: buran
  Tkinter - The Reuleaux Triangle andrewapk 1 1,875 Oct-06-2020, 09:01 PM
Last Post: deanhystad
  Python - networkx - Triangle inequality - Graph Nick_A 0 2,054 Sep-11-2020, 04:29 PM
Last Post: Nick_A
  making a guessing number game blacklight 1 2,154 Jul-02-2020, 12:21 AM
Last Post: GOTO10
  Triangle function program m8jorp8yne 2 8,802 Dec-13-2019, 05:24 PM
Last Post: Clunk_Head
  Print triangle using while loop tuxandrew 3 4,840 Dec-05-2019, 07:17 PM
Last Post: micseydel
  Intersection of a triangle and a circle Gira 3 3,541 May-19-2019, 06:04 PM
Last Post: heiner55
  Draw isosceles triangle fen1c5 4 12,682 Jul-07-2018, 10:20 AM
Last Post: fen1c5

Forum Jump:

User Panel Messages

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