Home > Net >  I am just stuck in python at printing a pattern. Please help me out
I am just stuck in python at printing a pattern. Please help me out

Time:10-06

I am a beginner in python programming and currently practicing to strong topic 'loop'. A question that say print following pattern. A B C D E F G H I J and so on...

So I done that, but i want to go further much advance level.

Ans.

x = int(input("How much rows you want to print:\n"))
letter = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
k= 0
for row in range(x):
    for col in range(row 1):
        print(letter[k],end=" ")
        k =1
        if k==len(letter):
            k ==0
    print()

but when i enter 7 or higher number i got an error this.

print(letter[k],end=" ")
list index out of range.

So As per my knowledge it's error because of 'k'. When 'k' reaches len(letter) list was end so I try to fix with if statement that reset value of k to 0 when its reaches len(letter). but I still getting same error. please tell me what I am doing wrong.

CodePudding user response:

This is a nice job for an iterator that you could slice with itertools.islice:

setup:

letter = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

## NB. you could use ascii.ascii_uppercase
# from string import ascii_uppercase
# letter = list(ascii_uppercase)

n = 6
from itertools import islice

iter_letters = iter(letter) # make a iterator

for i in range(n 1):
    print(' '.join(islice(iter_letters, i)))

output:

A
B C
D E F
G H I J
K L M N O
P Q R S T U

output for 7 lines:

A
B C
D E F
G H I J
K L M N O
P Q R S T U
V W X Y Z

NB. like for the list approach, you are limited here to 7 lines, or you will run out of letters

avoiding the 7 lines limit:

You can even make your list cycle using itertools.cycle:

from itertools import islice, cycle

n = 15

iter_letters = cycle(letter)

for i in range(n 1):
    print(' '.join(islice(iter_letters, i)))

output:


A
B C
D E F
G H I J
K L M N O
P Q R S T U
V W X Y Z A B
C D E F G H I J
K L M N O P Q R S
T U V W X Y Z A B C
D E F G H I J K L M N
O P Q R S T U V W X Y Z
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z A
B C D E F G H I J K L M N O P

CodePudding user response:

The array letter has 26 elements. So you can specify index from 0 to 25. The error you showed is raised when index is bigger than 26.

To handle index properly, you can implement like this print(letter[k % 26], end= " ")

% Can calculate the remainder, and numbers above 26 are adjusted to the corresponding numbers below 26

Please try this one.

CodePudding user response:

If you use list comprehension, the code will be more short and looks smart.

for row in range(x):
    print(" ".join([letter[(k col)%26] for col in range(row 1)]))
    k  = row   1

This list comprehension replaces second for statement into one liner. To use list comprehension, you will be able to code more like Python. But you don't have to use this technique. Please use this code if you want!!

  • Related