If you want to iterate through the characters of a string and at the same time get the position of each one, just use enumerate
. In a nutshell, I would:
palavra = input('palavra para soletrar:')
for i, letra in enumerate(palavra, start=1):
print(f'A {i}ª letra é {letra}')
Thus, with each iteration, letra
will be one of the characters in the string and i
will be their position. Only by default, enumerate
starts counting indexes at zero, but I can indicate a different initial value by passing the argument start
.
And in your case you’re making one loop to ask for several words, and can improve a little. For example, do not need to put a input
before the while
and another within it. If you need for example to change the message of input
or something like that, you’ll have to change in 2 places. So do it all just once, inside the loop.
And how is a loop infinite, it would be interesting to have some stop condition. For example, if the typed word is empty (the user only type one ENTER), you could terminate the while
:
while True:
palavra = input('palavra para soletrar:')
if not palavra: # se não foi digitado nada
break # sai do while
print(f'soletrando "{palavra}"')
for i, letra in enumerate(palavra, start=1):
print(f'A {i}ª letra é {letra}')
Of course you can use any logic you want to interrupt the program (like the example in another answer). But the main point is that you don’t need to repeat the input
outside and inside the loop.
And if you want the output to have the ordinal numbers spelled out, an alternative is to install the module num2words
:
from num2words import num2words
while True:
palavra = input('palavra para soletrar:')
if not palavra:
break
print(f'soletrando "{palavra}"')
for i, letra in enumerate(palavra, start=1):
print(f'O {num2words(i, to="ordinal", lang="pt_BR")} caractere é {letra}')
The problem is that the num2words
does not have option for the ordinal numbers in the feminine (it only returns "first" and there is no option to return "first"). So the way is to do the gambit:
def ordinal_fem(i):
s = num2words(i, to="ordinal", lang="pt_BR")
return s[:-1] + 'a' # troca a última letra por "a"
There in the loop just use the above function:
for i, letra in enumerate(palavra, start=1):
print(f'A {ordinal_fem(i)} letra é {letra}')
Or you can search for other libs that treat these cases (in a quick search, I found this, but I didn’t get to test).
It is also worth remembering that input
accepts anything that is typed by the user, and may even be a multi-word phrase, or only spaces or things that are not necessarily words (such as 123
, !@#$%
, etc). Of course, it is already outside the scope of the question, but if you want additional material on the subject, you can take a look here, here and here.
Do not set the amount of letters, vary according to the amount of letters of the given word.
– anonimo
If the answer solved your problem, you can
aceitá-la
, by clicking onV
next to the answer. See here why accept. Although not mandatory, this practice is encouraged on the site, indicating to future visitors that such a response solved the problem. And when you get 15 points, you can also vote in any useful responses.– Solkarped