Could someone explain why this error appears? the program asks for a word, and makes a pyramid with the word.
palavra = input('Digite a palavra:').upper()
lista = list(palavra)
x = 0 #ímpares
y = 0 #contador
z = len(palavra) #conta o numero de letras mais o add
while y < z:
if (x%2) == 0:
x = 1
else:
p = lista[y]*x
print(p.rjust(z))
x = 1
y = 1
z = 1
CodePudding user response:
I think z shouldn't be increased in the while loop other wise you will never end the while loop
palavra = input('Digite a palavra:').upper()
lista = list(palavra)
x = 0 #ímpares
y = 0 #contador
z = len(palavra) #conta o numero de letras mais o add
while y < z:
if (x%2) == 0:
x = 1
else:
p = lista[y]*x
print(p.rjust(z))
x = 1
y = 1
# z = 1
CodePudding user response:
This will fix your error. Try this,
palavra = input('Digite a palavra:').upper()
lista = list(palavra)
print(lista)
x = 0 #ímpares
y = 0 #contador
z = len(palavra) #conta o numero de letras mais o add
while y < z:
if (x%2) == 0:
x = 1
else:
if(len(palavra)-1 < y): #Check the length of string and y value to end the loop
break
p = lista[y]*x
print(p.rjust(z))
x = 1
y = 1
z = 1
Output like this,
CodePudding user response:
You are incrementing 'y' in iteration & that's make the issue. And the last iteration the it will always pointing to length of the string and list index on that time doesn't exist
palavra = input('Digite a palavra:').upper()
lista = list(palavra)
x = 0 #ímpares
y = 0 #contador
z = len(palavra) #conta o numero de letras mais o add
strlen = len(palavra) #conta o numero de letras mais o a
while y < strlen:
if (x%2) == 0:
x = 1
else:
p = lista[y]*x
print(p.rjust(z))
x = 1
y = 1
z = 1
CodePudding user response:
palavra = input('Digite a palavra:').upper()
lista = list(palavra)
x = 0 #ímpares
y = 0 #contador
z = len(palavra) #conta o numero de letras mais o add
try:
while y < z:
if (x%2) == 0:
x = 1
else:
p = lista[y]*x
print(p.rjust(z))
x = 1
y = 1
z = 1
except:
pass
#output
Digite a palavra:Talha
T
AAA
LLLLL
HHHHHHH
AAAAAAAAA
CodePudding user response:
Your code is ineffiecient and not working properly.
The first thing to do is to use functions, so that you can reuse your code.
The second thing you should do is to use str.center
method.
The mathematical relationship between the indices and the strings is that each string has the length of its index times two (0 based indexing).
So I reimplemented your code:
from string import ascii_lowercase
def pyramidize(s):
l = (len(s) - 1) * 2 1
return '\n'.join([(c * (i * 2 1)).center(l) for i, c in enumerate(s)])
print(pyramidize(ascii_lowercase))
a
bbb
ccccc
ddddddd
eeeeeeeee
fffffffffff
ggggggggggggg
hhhhhhhhhhhhhhh
iiiiiiiiiiiiiiiii
jjjjjjjjjjjjjjjjjjj
kkkkkkkkkkkkkkkkkkkkk
lllllllllllllllllllllll
mmmmmmmmmmmmmmmmmmmmmmmmm
nnnnnnnnnnnnnnnnnnnnnnnnnnn
ooooooooooooooooooooooooooooo
ppppppppppppppppppppppppppppppp
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
sssssssssssssssssssssssssssssssssssss
ttttttttttttttttttttttttttttttttttttttt
uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz