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

Asked

Viewed 76 times

0

In a in C, we define "beginning, end and step increment". I know that in Python it is not so. I saw that it is called . I wish the end of mine is defined by the user. And that, at each iteration until the end of the is, a value is requested from the user and saved in an array.

I tried to:

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 didn’t work because it never ends (it actually makes a mistake when it reaches entry 101, because it doesn’t fit in the vector anymore, but if it weren’t 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 a better that a , unless you’re going to iterate initamente (ie never). I read this in "Learn Python The Hard Way".

Anyway, neither one of you is working the way I’d like.

  • I see you’re wearing raw_input. Which python version you are using ?

2 answers

2


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

Say you wanted to do a go to count from 0 to 9, in C you would do it this way:

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

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

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

Note that in Python, I use a range of values to go through, in this case, from 0 to 9, these values can be replaced 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 2 by 2. Moving on to your question... In Python we have one thing that in my opinion helps a lot, are 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 helped you with something :D

  • Thank you very much, it worked out! I had tried something like "for c in size:", so it didn’t work.

  • Just a question, why the "str" in "value = input('Type the tracus ' + str(c+1) + ' for training:')"? I thought I could do "value = input('Type stroke ', c+1, 'for training:'). No?

  • Ah, I use Python 2 (2.7.14) and it worked.

  • The variable 'c' is of type int, if we want to concatenate it with a string we first need to convert it to string, so the use of str()'. When you use commas to concatenate as "input('Type tracer ', c+1, ' for training:')" in this case you are not concatenating, but passing three parameters to the input() function and Python will complain, on the other hand this works well in print().

  • Yes, I imagined. Even in the print it was a problem, sometimes I said I didn’t give paw to concatenate str, sometimes no, will understand.

0

In order to be able to implement what you have in mind you must do the following:

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

this definition could possibly be something like (pseudo-code structure):

  • continue while input variable is different from 0

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

  • continue (yes - 1, no - 0) - print to console
  • should then read the input for a variable that should be the same as the one in the cycle header when it says it will continue if it is different from 0

Browser other questions tagged

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