Answer to Question #167914 in Python for srikanth

Question #167914
Write a program to print the right alphabetic triangle up to the given N rows.
Input

The input will be a single line containing a positive integer (N).
Output

The output should be N rows with letters.
Note: There is a space after each letter.
Explanation

For example, if the given number of rows is 4,
your code should print the following pattern.
A 
A B 
A B C 
A B C D

Sample Input 1
4
Sample Output 1
A 
A B 
A B C 
A B C D 

Sample Input 2
6
Sample Output 2
A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F 


1
Expert's answer
2021-03-03T08:40:22-0500
import string

# Get user input
N = int(input("Please enter a positive integer (N): "))

for i in range(N):
   # Create slice of uppercase letters starts with 'A' and to the current 'i' value + 1
   slice = string.ascii_uppercase[0: i + 1]
   # Join each element of the slice with space and print
   print(" ".join(slice))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS