How to use alphabet range in python?

Asked

Viewed 113 times

-2

I am trying to make a program that makes the letters of the alphabet in triangle type so the user enters with an integer number and the result is displayed in triangle form.

7:
A

BB

CCC

DDDD

EEEEE

FFFFFF

GGGGGGG

4:
A

BB

CCC

DDDD

I’ve tried wearing the bow tie for but I couldn’t. Follow my attempt:

N = int(input())

if N == 1:
    print('A')
if N == 2:
    print('A\n'
          'BB')
if N == 3:
    print('A\n'
          'BB\n'
          'CCC')
if N == 4:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD')
if N == 5:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE')
if N == 6:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF')
if N == 7:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG')
if N == 8:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH')
if N == 9:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n')
if N == 10:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ')
if N == 11:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n')
if N == 12:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n')
if N == 13:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n')
if N == 14:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN')
if N == 15:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n')
if N == 16:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n')
if N == 17:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n'
          'QQQQQQQQQQQQQQQQQ\n')
if N == 18:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n'
          'QQQQQQQQQQQQQQQQQ\n'
          'RRRRRRRRRRRRRRRRRR\n')
if N == 19:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n'
          'QQQQQQQQQQQQQQQQQ\n'
          'RRRRRRRRRRRRRRRRRR\n'
          'SSSSSSSSSSSSSSSSSSS\n')
if N == 20:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n'
          'QQQQQQQQQQQQQQQQQ\n'
          'RRRRRRRRRRRRRRRRRR\n'
          'SSSSSSSSSSSSSSSSSSS\n'
          'TTTTTTTTTTTTTTTTTTTT\n')
if N == 21:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n'
          'QQQQQQQQQQQQQQQQQ\n'
          'RRRRRRRRRRRRRRRRRR\n'
          'SSSSSSSSSSSSSSSSSSS\n'
          'TTTTTTTTTTTTTTTTTTTT\n'
          'UUUUUUUUUUUUUUUUUUUUU\n')
if N == 22:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n'
          'QQQQQQQQQQQQQQQQQ\n'
          'RRRRRRRRRRRRRRRRRR\n'
          'SSSSSSSSSSSSSSSSSSS\n'
          'TTTTTTTTTTTTTTTTTTTT\n'
          'UUUUUUUUUUUUUUUUUUUUU\n'
          'VVVVVVVVVVVVVVVVVVVVVV\n')
if N == 23:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n'
          'QQQQQQQQQQQQQQQQQ\n'
          'RRRRRRRRRRRRRRRRRR\n'
          'SSSSSSSSSSSSSSSSSSS\n'
          'TTTTTTTTTTTTTTTTTTTT\n'
          'UUUUUUUUUUUUUUUUUUUUU\n'
          'XXXXXXXXXXXXXXXXXXXXXXX\n')
if N == 24:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n'
          'QQQQQQQQQQQQQQQQQ\n'
          'RRRRRRRRRRRRRRRRRR\n'
          'SSSSSSSSSSSSSSSSSSS\n'
          'TTTTTTTTTTTTTTTTTTTT\n'
          'UUUUUUUUUUUUUUUUUUUUU\n'
          'XXXXXXXXXXXXXXXXXXXXXXX\n'
          'WWWWWWWWWWWWWWWWWWWWWWWW\n')
if N == 25:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n'
          'QQQQQQQQQQQQQQQQQ\n'
          'RRRRRRRRRRRRRRRRRR\n'
          'SSSSSSSSSSSSSSSSSSS\n'
          'TTTTTTTTTTTTTTTTTTTT\n'
          'UUUUUUUUUUUUUUUUUUUUU\n'
          'XXXXXXXXXXXXXXXXXXXXXXX\n'
          'WWWWWWWWWWWWWWWWWWWWWWWW\n'
          'YYYYYYYYYYYYYYYYYYYYYYYYY\n')
if N == 26:
    print('A\n'
          'BB\n'
          'CCC\n'
          'DDDD\n'
          'EEEEE\n'
          'FFFFFF\n'
          'GGGGGGG\n'
          'HHHHHHHH\n'
          'IIIIIIIII\n'
          'JJJJJJJJJJ\n'
          'KKKKKKKKKKK\n'
          'LLLLLLLLLLLL\n'
          'MMMMMMMMMMMMM\n'
          'NNNNNNNNNNNNNN\n'
          'OOOOOOOOOOOOOOO\n'
          'PPPPPPPPPPPPPPPP\n'
          'QQQQQQQQQQQQQQQQQ\n'
          'RRRRRRRRRRRRRRRRRR\n'
          'SSSSSSSSSSSSSSSSSSS\n'
          'TTTTTTTTTTTTTTTTTTTT\n'
          'UUUUUUUUUUUUUUUUUUUUU\n'
          'XXXXXXXXXXXXXXXXXXXXXXX\n'
          'WWWWWWWWWWWWWWWWWWWWWWWW\n'
          'YYYYYYYYYYYYYYYYYYYYYYYYY\n'
          'ZZZZZZZZZZZZZZZZZZZZZZZZZZ\n')

How do I get this same result using loop for?

  • 2

    Could you show us how you did with the bow tie for?

  • Lucas, I edited your question in an attempt to make it clearer. If you don’t approve the modifications you can enter edition history and reverse the modifications.

  • 1

    https://ideone.com/AkNqFj

2 answers

3


The module string from the standard python library defines some constants among which string.ascii_uppercase is the sequence of capital letters ABCDEFGHIJKLMNOPQRSTUVWXYZ.

It is possible enumerate and iterate for a slice, the size chosen by the user, the sequence of letters and each iterated element the print repeatedly with the help of the operator * sequential repeat:

import string

N = int(input("Digite a altura da pirâmide:"))
#Itera pela fatia de caracteres onde i é índice e c é o caractere com o índice começando em 1... 
for i,c in enumerate(string.ascii_uppercase[:N], 1):
  print(c*i);         #...imprime o caractere o repetindo i vezes.

Test the code on Ideone

0

For this you can use the global functions of Python as a chr.

# Quantidade de linhas
value = int(input("Digite: "))

# Executa a quantidade de vezes digitada
# inicialmente, para cada nova iteração
# ele printa uma nova letra
for i in range(value):
    for j in range(i + 1):
        # Mostra o caracter n vezes
        print(chr(65 + i), end="") # 65 = 'A' na tabela ASCII
    print("")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.