How can the end of "for" be a user-defined variable?

0

In a in C, we define "start, end and increment step. " I know Python is not like this. I saw it called . I wish the end of my to be defined by the user. And that, at each iteration until the end of for, a value was requested from the user and saved in a vector.

I tried:

num_tracos = raw_input()

tracos_treino = np.arange(100)

for i in num_tracos:
    print "Digite o traco", i+1, " para o treinamento"
    tracos_treino[i] = raw_input()

I also tried a , but it did not work because I never ends (it actually gives error when it reaches entry 101, because it no longer fits into the vector, but if it were not for that, it would be infinite):

num_tracos = raw_input()

 tracos_treino = np.arange(100)

i=0
while i < num_tracos :
    print "Digite o traco", i+1, " para o treinamento"
    tracos_treino[i] = raw_input()
    i = i+1

I've always heard that that a , unless you go iterate (ie never) . I read this in "Learn Python The Hard Way."

Anyway, neither is working the way I would.

    
asked by anonymous 12.05.2018 / 11:39

2 answers

2

What version of Python are you using? I'll give my idea on top of Python 3. In fact the in Python is a bit different than in C, but you can do these tasks without problems, just understand the structure.

Let's say you want to make a for to count from 0 to 9, in C you would do this as follows:

    for(int c=0; c<10; c++) {
        printf("%d\n", c);
    }

In Python this would look a bit different, see below:

    for c in range(0, 10):
        print(c)

Notice that in Python, I use a range of values to go through, in this case from 0 to 9, these values can be overridden by variables. You can also set the iteration step.

    for c in range(0, 10, 2):
        print(c)

Notice the third parameter of the range, it makes the count happen by 2 in 2. Leading to your question ... In Python we have something that in my opinion helps a lot, it's the lists.

    # Definimos uma lista para guardar os valores.
    tracos_treino = []
    # Podemos solicitar ao usuário quantos ítens ele quer inserir na lista.
    tamanho = int(input('Informe a quantidade de ítens desejados:'))
    # Fazemos a iteração.
    # Aqui utilizo a variável 'tamanho' para determinar o fim da contagem.
    for c in range(0, tamanho):
        valor = input('Digite o traco ' + str(c+1) + ' para o treinamento:')
        tracos_treino.append(valor)   # Adiciona ao final da lista.
    # Depois podemos fazer a exibição desses valores.
    for c in range(0, tamanho):
        print(tracos_treino[c])

I hope I have helped with something: D

    
13.05.2018 / 11:06
0

To be able to implement what you have in mind you have to do the following:

  • create a condition to end the cycle while , in the definition of it
  • create input on the console to read an input variable
  • From there, map the input variable to the condition that closes cycle while loop

This definition might be something of the sort (pseudo-code structure):

  • continue while input variable is different from 0

There, at the end of each iteration cycle you should put the following:

  • want to continue (yes - 1, no - 0) - print to the console
  • You should then read the input for a variable that should be the same as that found in the cycle header when it says it continues if it is not 0
12.05.2018 / 13:23