Sep-24-2022, 09:35 AM
(This post was last modified: Sep-24-2022, 09:52 AM by Yoriz.
Edit Reason: Fixed code tags
)
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.
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?