How to start a range from 1

Asked

Viewed 1,815 times

4

I wish on the part of

input("Put student’s note " + str(notes) + ":" )

the computer ask "Enter student’s grade 1: " and not starting from 0, how to do this with the range function?

Code below.

Alunos = int((input("Digite quantos alunos você deseja cadastrar: ")))
quantidadeAlunos = (list(range(Alunos)))

for notas in quantidadeAlunos:
    input("Coloque a nota do aluno " + str(notas) + ":" )
  • Note that you do not need to coerce the range in a list to be able to iterate. The range is a generator (yielder), a cool stop that can handle a list of 5 million elements (or more!, has examples of Python generators that go to infinity) using almost nothing in memory

3 answers

5


Just increment the counter:

alunos = int(input("Digite quantos alunos você deseja cadastrar: "))
notas = []

for i in range(1, alunos + 1):
    nota = input("Coloque a nota do aluno " + str(i) + ":" )
    notas.append(nota)
  • 1

    Gypsy, you are losing/not considering the last student. Remember that the interval of the range is opened in the stop

  • 1

    It’s true. Corrected.

  • 1

    Something in your answer still smells strange to me... like, do you declare that notas is an empty list on line 3, so on line 5 you say that notas will be used to iterate at the desired interval, transforming its value into integer; hence, at line 7 you ask that the read value be appended at the end of the integer notas, to then iterate again to the next element of range (line 5) and lose the value just appended... I believe you wanted to iterate on another variable other than notas, nor nota, check? I believe iterate on aluno_atual is enough by removing lines 2 and 8

  • I didn’t understand a thing.

  • Do you have a Python terminal? Print the value of notas at the beginning and end of each iteration. Unfortunately, I don’t have one on my machine and I won’t open one now in the browser

  • 1

    Corrected again. It’s 6a at night and I’m already in the 3rd beer.

  • Bro, it’s almost Saturday and I have to deliver ten screens of a prototype for a post work until Sunday =/ All right, I can’t concentrate, so I come to OS to discontrair

  • 1

    I’m like that too. Writing course chapter until about 2 in the morning in the time here. There is a lot of response from here that I put in the course. Good luck to us.

Show 3 more comments

5

Just put +1 after the i or notas in your case.

The variable quantidadeAlunos does not need to exist, the variable Alunos already does that.

Thus remaining:

Alunos = int(input("Digite quantos alunos você deseja cadastrar: "))

for i in range(Alunos):
    notas = input("Coloque a nota do aluno " + str(i+1) + ":" )

3

There are 3 ways to call the range in Python:

  1. range(INI, FIM, INC), this is, shall we say, the basic form of range in Python. The range starts with value INI, being incremented by the value INC at each iteration until it reaches or exceeds the value of FIM; follows the following formula, to i rising from scratch: descrição matemática do conjunto
  2. range(INI, FIM), in this case, it is assumed that the increment value INC be unitary;
  3. range(FIM); in this case, it is assumed that the start INI is 0 and the increment INC be unitary.

It is always worth noting that the interval is closed at the beginning (i.e., includes INI) and open at the end (i.e., it does not arrive in FIM, for exactly in the previous step).

Taking possession of this knowledge, use the best semantics for your problem. In your particular case, I believe you wish to use these values only for user interaction, not in the internal logic of your program. If this is the case, the ideal is to do as the answer of Anthony Gabriel: only the display is treated to inform user-friendly indexes, making the use of the internal index more friendly to the code.

UPDATE

In case the iteration does not favor the internal logic of the iteration, the Gypsy Morrison Mendez is more performative and direct to the point.

I must admit that I am biased to start ties by zero, so by pure prejudice I would lose this micro optimization.

Browser other questions tagged

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