How to start a range from 1

2

I wish that in the

  

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

The computer asks " Put student 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) + ":" )
    
asked by anonymous 13.05.2017 / 05:47

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)
    
13.05.2017 / 05:53
4

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

The variable quantidadeAlunos does not have to exist, the variable Alunos already does this.

Looking like this:

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

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

There are 3 ways to call range in Python:

  • range(INI, FIM, INC) , this is, let's say, the basic form of range in Python. The interval starts with INI , being incremented by the INC value at each iteration until it reaches or exceeds the value of FIM ; follows the following formula for i increasing starting from zero:
  • range(INI, FIM) , in this case, it is assumed that the increment value INC is unitary;
  • range(FIM) ; in this case, it is assumed that the start INI is 0 and that the INC increment is unitary.
  • It should always be noted that the range is closed at the beginning (that is, includes INI ) and open at the end (ie it does not reach FIM , for exactly the previous step).

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

    UPDATE

    If the iteration does not favor the internal logic of the iteration at all, the answer from the Cigano Morrison Mendez is more and direct to the point.

    I must admit that I'm biased to starting ties for zero, so out of pure prejudice I would lose this micro-optimization.

        
    13.05.2017 / 07:32