how can I do for while to repeat the variable 'name' a certain number of times?

0
def fun ():
    re_ingresar = True
    num_nombres = int(input('cuantos nombres vas a escribir?'))
    while (re_ingresar):
        nombre = input('escribe un nombre:')
#el output te repite 'escribe un nombre' infinidad de veces,
#pero estoy tratando que sea solo 3 veces osea (num_nombres veces). 

What I try is this:

def fun ():
    re_ingresar = True
    num_nombres = int(input('cuantos nombres vas a escribir?'))
    while (re_ingresar):
        name = input('escribe un nombre:')

        if repeticion == num_nombres #la variable 'repeticion' es un ejemplo y no se me ocurre como encontrarla
                                     #repeticion = numero de veces que la variable name se repite.
        re_ingresar = False

My question is: Is it possible to give a value to the variable 'repetition'. If not, what would you advise me?

Thanks in advance.

    
asked by juan 05.11.2018 в 02:18
source

1 answer

2

I recommend using for i in range(num_nombres) given that it is more in line with the problem. If for some reason you were forced to use while define the variable i = 0 out of the loop and increment it in each iteration. When it is fulfilled that i == num_nombres sets re_ingresar as False to exit the loop.

def fun (): 
    re_ingresar = True 
    num_nombres = int(input('cuantos nombres vas a escribir?')) 
    i = 0
    while (re_ingresar): 
        name = input('escribe un nombre:') 
        if i == num_nombres:
            re_ingresar = False
        i += 1
    
answered by 05.11.2018 / 02:38
source