Execute the for by changing a piece of string

1

Good friends, Greetings. The fact is that I am starting in the prog. And testing I found something curious. First look at the code and then the doubt. Thanks in advance

def body():
    print(temps)




if __name__ == '__main__':
    temps=list()
    for i in range(0,7):
        if i==0:
            datoIngresado=int(input("Ingrese la primera temperatura:  "))
            temps.append(datoIngresado)
    elif i==1:
            datoIngresado=int(input("Ingrese la segunda temperatura:  "))
            temps.append(datoIngresado)
        elif i==2:
            datoIngresado=int(input("Ingrese la tercera temperatura:  "))
            temps.append(datoIngresado)
        elif i==3:
            datoIngresado=int(input("Ingrese la cuarta temperatura:  "))
            temps.append(datoIngresado)
        elif i==4:
            datoIngresado=int(input("Ingrese la quinta temperatura:  "))
            temps.append(datoIngresado)
        elif i==5:
            datoIngresado=int(input("Ingrese la sexta temperatura:  "))
            temps.append(datoIngresado)
        elif i==6:
            datoIngresado=int(input("Ingrese la septima temperatura:  "))
            temps.append(datoIngresado)

body()

The fact is that I want to make a for loop to request 7 data equivalent to 7 days a week. But as you can see, I have to use 7 structures if comparing them with the variable i of my for cycle. I do this to have to show on screen a different string from the others. That is, pass "enter the first data" and so for the second input "Enter the second data." Where does my doubt come from? Well I try to make a for cycle in which each time it is executed I change in that word (that if first, second and so on, advance the for loop) without having to use so many if structures. Is it possible?

    
asked by limg21 17.05.2017 в 04:41
source

2 answers

1

If possible, it is ... If you are looking for a better way to do it, here is an alternative:

temps = list()
dias  = [ "primera", "segunda", "tercera", "cuarta", "quinta", "sexta", "septima" ]

def body():
    print(temps)

if __name__ == '__main__':
    for i in range(7):
        datoIngresado = int(input('Ingrese la %s temperatura: ' % dias[i]))
        temps.append(datoIngresado)
    body()

This basically adjusts the possible 7 values that you are going to enter (Assuming that they will always be 7, otherwise I recommend putting only the number).

Using a list días of the temperature you ask for, just access the index [i] without leaving aside the logic you implement.

    
answered by 17.05.2017 / 04:51
source
1

It is enough to go through an iterable with the content of the input or its modification for each data entry. In this case, str.format () is used to format the string and the use of indices that are more inefficient and less "pythonic" than the for in is dispensed with:

temps=list()

for p in ('primera', 'segunda', 'tercera', 'cuarta', 'quinta', 'sexta', "séptima"):
    datoIngresado = int(input('Ingrese la {} temperatura: '.format(p))
    temps.append(datoIngresado)

If you use Python> = 3.6 you can use the formatted string literals :

temps=list()

for p in ('primera', 'segunda', 'tercera', 'cuarta', 'quinta', 'sexta', "séptima"):
    datoIngresado = int(input(f'Ingrese la {p} temperatura: '))
    temps.append(datoIngresado)
    
answered by 17.05.2017 в 11:34