Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
triangle numbers
#1
Sad 
Hi all, I want to print the output as below:-
Output:
1 X2 XX3 XXX4 XXXX5 XXX4 XX3 X2 1

I tried to solve the problem above as below:-
line = '12345'
for i in range(1,6):
    print(line[-i:])
for i in range(1,6):
     print(line[i:])
output:
Output:
5 45 345 2345 12345 2345 345 45 5
how it works in crop what I want? can anyone help?
Huh Huh
Yoriz write Sep-24-2022, 09:54 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The term is "slice", not "crop". Your pyramid program is printing slices o line.

You should not have "magic numbers" in you code like 6. If you change line to "123456", you should not have to know that you must also change 6 to 7. The program should do that for you. Python has a function that returns the number of items in a list. Plus, I don't think you want to use 6. 6 doesn't produce the correct result.
line = 'ABCDE'
for i in range(1, len(line)):
    print(line[-i:])
for i in range(len(line)):
    print(line[i:])
Output:
E DE CDE BCDE ABCDE BCDE CDE DE E
If you only want to print 1 element should use indexing, not slicing. This prints elements starting from the end, working tot the start, and then back to the end.
line = 'ABCDE'
for i in range(1, len(line)):
    print(line[-i])
for i in range(len(line)):
    print(line[i])
Output:
E D C B A B C D E
How could this be modified to print elements beginning at the start, moving to the end, and then back to the start?
Reply
#3
thanks for your help.
actually, I want to have the output like as below:-

1
• 2
••3
•••4
••••5
•••4
••3
•2
1
Reply
#4
Are the dots meant to be spaces? That would explain why the list looked wrong in your original post. That is even easier.
for i in range(5):
    print(f"{spaces}{i+1}")
for i in range(4, 0, -1):
    print(f"{spaces}{i}")
Now figure how to make "spaces".
Reply
#5
(Sep-24-2022, 03:37 PM)deanhystad Wrote: Are the dots meant to be spaces? That would explain why the list looked wrong in your original post. That is even easier.
for i in range(5):
    print(f"{spaces}{i+1}")
for i in range(4, 0, -1):
    print(f"{spaces}{i}")
Now figure how to make "spaces".

cool.
Yes. the dots are meant to be spaces.

tried to use the code, it shows NameError: name 'spaces' is not defined.
can you help?
what is the difference between {} and ()?
Reply
#6
Replacing "spaces" is an exercise left to the reader. I would read about the str datatype and what methods it supports. I bet there is a method for padding a string to some width. I'd also do some research on f'string formatting. There is a way to set the width of a string when it is printed. Very useful for printing out tables and having the columns line up. There is also an operator that you can use on strings to make a longer string by concatenating a short string some multiple number of times.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fill a value in triangle shape in Matrix lynx 0 1,880 Dec-07-2019, 06:32 AM
Last Post: lynx
  Hollow triangle-drawing characters param error. phob0s 4 2,607 Jul-31-2019, 08:18 AM
Last Post: phob0s
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,735 May-09-2019, 12:19 PM
Last Post: Pleiades
  Printing out a triangle using nested for loops MrGoat 12 6,573 Jan-16-2019, 07:21 PM
Last Post: ichabod801
  Triangle: max path sum Mateoo 1 2,932 Jan-10-2019, 09:16 PM
Last Post: stullis
  Area of a triangle OmarSinno 8 5,755 Sep-25-2017, 08:10 PM
Last Post: OmarSinno

Forum Jump:

User Panel Messages

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