The first problem (does not update the serial number) happens because you are not incrementing the counter i inside the loop.
The second problem is solved by checking the size of the typed string, through the function len.
The third problem (show series) happens because of this loop:
for i in series:
print("{}: {}".format(i, series))
The variable series is a list containing all series. When doing for i in series, every iteration of loop, the variable i contains the name of one of the series. But you print the name (the variable i) and the whole list (the variable series), so the way out is the way you indicated.
Other details for tidying up:
- the variable
series starts as a list containing an empty string (series = ['']). But there is no reason for it, just assign the value [] to start the list empty
- the function
input already returns a string, it is not necessary to use str(input(...))
- if you want to iterate through the list elements along with their index, just use
enumerate
The code goes like this:
nome = ''
while not nome:
nome = input("Digite seu nome: ").strip()
print("{}, quero lembrar que não é permitido digitar apenas uma letra".format(nome))
print("Deseja responder a pergunta? ")
resposta = input("S - Sim | N - Não: ").strip()[0]
i = 1
series = []
while resposta.lower() == 's':
while True:
serie = input("Digite a sua {}a série favorita: ".format(i)).strip()
if len(serie) <= 1:
print('Nome deve ter mais de uma letra')
else:
break # nome OK, sai do while True
# o while True acima já garante que o nome estará preenchido
print("Nome da série: {}".format(serie))
series.append(serie)
i += 1
print("Deseja digitar mais alguma série, {}?".format(nome))
resposta = input("S - Sim | N - Não: ").strip()[0]
if resposta.lower() == 'n':
print("{}, Bye!".format(nome))
for i, serie in enumerate(series):
print("{}: {}".format(i + 1, serie))
Other details:
- used
strip() to remove the start and end spaces of the strings returned by input (is not strictly necessary, use if you want)
- the loop
while True already ensures that the series name must be filled in, so I don’t need to test the name before printing it
- to check the option, I use
resposta.lower() == 's' - for lower() turns the string into lowercase. But if you want, you can also do while resposta in ('S', 's'):.
while not nome is the same that while nome == ""
You can also break each part of the code into functions (one to read the series name, another to the options, etc), so the code gets a little more organized:
def ler_opcao(mensagem, nome=None):
if nome:
print('{}, {}? '.format(mensagem, nome))
else:
print('{}? '.format(mensagem))
# já retorna a opção como letra minúscula
return input("S - Sim | N - Não: ").strip()[0].lower()
def ler_nome_serie(i):
while True:
serie = input("Digite a sua {}a série favorita: ".format(i)).strip()
if len(serie) <= 1:
print('Nome deve ter mais de uma letra')
else:
return serie # nome OK, retorna o nome da série
nome = ''
while not nome:
nome = input("Digite seu nome: ").strip()
print("{}, quero lembrar que não é permitido digitar apenas uma letra".format(nome))
resposta = ler_opcao("Deseja responder a pergunta")
i = 1
series = []
while resposta == 's':
serie = ler_nome_serie(i)
print("Nome da série: {}".format(serie))
series.append(serie)
i += 1
resposta = ler_opcao("Deseja digitar mais alguma série", nome)
if resposta == 'n':
print("{}, Bye!".format(nome))
for i, serie in enumerate(series):
print("{}: {}".format(i + 1, serie))
Another detail is the if resposta == 'n':. The while only checks if the option is s. So if you type, for example, x, he also comes out of while, but then it doesn’t enter the if (because it wasn’t typed n). If you want to always print the "Bye" message, simply remove the if.
Some things you explained will be of great help, thank you very much.
– user141036