"print()" does not display the elements of the inserted list

Asked

Viewed 59 times

0

Why does the instruction below not work? It displays the last note, not the list of all notes.

jnotas = int(input("quantas notas você já tem = "))
 
for c in range (0, jnotas):
    notas1 = []
    n = float(input("entre com sua nota = "))
    notas1.append(n)
    
print(notas1)
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

2

You are starting the list inside the loop, so every time you pass by the list is reset, only save the last entered element.

quantidade = int(input("quantas notas você já tem = "))
notas = []
for c in range (0, quantidade):
    notas.append(float(input("entre com sua nota = ")))
print(notas)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • thank you very much ! I have to pay attention to these things !

0

As has already been answered anteriormente you are initiating the list within the loop for. In this way, every iteration of the for list elements are overwritten. Correct is you implement the list fora do for, as in the code below...

jnotas = int(input("quantas notas você já tem = "))

notas1 = []
for c in range(0, jnotas):
    n = float(input("entre com sua nota = "))
    notas1.append(n)

print(notas1)

Now, if you wish to give mais legibilidade to your code, you can implement the following code below...

quant_notas = int(input('Desejas inserir quantas notas? '))

notas = list()
for c in range(1, quant_notas + 1):
    n = float(input(f'Entre com sua {c}ª nota: '))
    notas.append(n)

print(f'\033[32mAs notas digitadas são: {notas}\033[m')

See here the functioning of the algorithm.

Something else, if you wish exibir cada valor por linha and without displaying the colchetes, you can use this code...

quant_notas = int(input('Desejas inserir quantas notas? '))

notas = list()
for c in range(1, quant_notas + 1):
    n = float(input(f'Entre com sua {c}ª nota: '))
    notas.append(n)

for nota in notas:
    print(f'\033[32m{nota}')

See here the functioning of the algorithm.

If instead you wish exibir the elements of the list in uma só linha, separated by vírgula and without delimiters colchetes, you can use the following code...

quant_notas = int(input('Desejas inserir quantas notas? '))

notas = list()
for c in range(1, quant_notas + 1):
    n = float(input(f'Entre com sua {c}ª nota: '))
    notas.append(n)

print(f'\033[32mAs notas são:')
for nota in notas:
    print(f'{nota}', end='')
    print(', ' if nota < notas[-1] else '. ', end='')

See here the functioning of the algorithm.

If that’s the case, you want to reduzir the amount of lines of code, you can use the next algorithm. In this algorithm, you will have to type todos values in the same row and separados for um space.

quant_notas = list(map(float, input('Insira aqui todas as notas separadas por um espaço: ').split()))
print(f'\033[32mAs notas são: {quant_notas}\033[m')

If you want to display the list elements in uma só linha, without vírgulas ou pontos and without delimiters colchetes, you can use this algorithm...

quant_notas = list(map(float, input('Insira aqui todas notas separadas por um espaço: ').split()))
print('A notas são: ', *quant_notas)

Now, if you wish to display cada value per line with a given espaçamento temporal, you can implement the following algorithm...

from time import sleep

quant_notas = int(input('Desejas inserir quantas notas? '))

notas = list()
for c in range(1, quant_notas + 1):
    n = float(input(f'Entre com sua {c}ª nota: '))
    notas.append(n)

cont = 0
for nota in notas:
    cont += 1
    sleep(0.4)
    print(f'\033[32mA {cont}ª nota é: {nota}') 

See here the functioning of this algorithm.

Browser other questions tagged

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