Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
triangle numbers
#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


Messages In This Thread
triangle numbers - by Woody_MC_2022 - Sep-24-2022, 09:10 AM
RE: triangle numbers - by deanhystad - Sep-24-2022, 09:35 AM
RE: triangle numbers - by Woody_MC_2022 - Sep-24-2022, 03:01 PM
RE: triangle numbers - by deanhystad - Sep-24-2022, 03:37 PM
RE: triangle numbers - by Woody_MC_2022 - Sep-24-2022, 03:52 PM
RE: triangle numbers - by deanhystad - Sep-24-2022, 08:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fill a value in triangle shape in Matrix lynx 0 2,479 Dec-07-2019, 06:32 AM
Last Post: lynx
  Hollow triangle-drawing characters param error. phob0s 4 3,425 Jul-31-2019, 08:18 AM
Last Post: phob0s
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 5,113 May-09-2019, 12:19 PM
Last Post: Pleiades
  Printing out a triangle using nested for loops MrGoat 12 8,906 Jan-16-2019, 07:21 PM
Last Post: ichabod801
  Triangle: max path sum Mateoo 1 3,514 Jan-10-2019, 09:16 PM
Last Post: stullis
  Area of a triangle OmarSinno 8 7,493 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