Request more than 10 data without using so many variables

4

I just started with this world of Python and I'm doing personal sample software.

But I need to ask for too much data.

EXAMPLE:

dato1 = float(input("Dígame su peso en kg: "))
dato2 = float(input("Dígame su peso en kg: "))
dato5 = float(input("Dígame su peso en kg: "))
dato6 = float(input("Dígame su peso en kg: "))
dato7 = float(input("Dígame su peso en kg: "))
dato8 = float(input("Dígame su peso en kg: "))
dato9 = float(input("Dígame su peso en kg: "))
dato10 = float(input("Dígame su peso en kg: "))

But if the user needs more than 10 data and I only have 10 variables, what can I do? What I could do easily is fill me with variables but I do not want that. I think there is another way.

    
asked by SAGC GC 22.04.2018 в 01:01
source

2 answers

4

Completing the response of @PatricioMoracho and in relation to your comment on it, if you want the user to define the number of data to enter you have two simple options:

  • Ask the user previously to enter the number of data you want to enter and pass it to range :

    n = int(input("¿Cuántos datos dese ingresar? "))
    datos = [float(input("Dígame su peso en kg: ")) for _ in range(n)]
    
  • Change the for for a while that is broken based on an exit condition that the user can provide, for example enter data until one of them has x value or that an empty string is entered:

    datos = []
    
    dato = input("Dígame su peso en kg: ")
    while dato:
        datos.append(float(dato))
        dato = input("Dígame su peso en kg: ")
    

In this case the user can enter data indefinitely (while there is available memory to be strict) until you enter an empty string, at which point the cycle is broken.

If you want to validate the entry (for example, to prevent anything that is not a positive number from being entered) and also to prevent the program from ending with an exception if the casting at float is not possible, you can use a accessory function:

def ingresar_peso():
    while True:
        try:
            cad = input("Dígame su peso en kg: ")
            if not cad:
                return None
            peso = float(cad)
            if peso < 0:
                raise ValueError()
            return peso
        except:
            print("Error, el peso debe ser un número positivo.")
            continue


datos = []

dato = ingresar_peso()
while dato:
    datos.append(float(dato))
    dato = ingresar_peso()

Execution example:

Dígame su peso en kg: 56
Dígame su peso en kg: dadda
Error, el peso debe ser un número positivo.
Dígame su peso en kg: 81
Dígame su peso en kg: -55
Error, el peso debe ser un número positivo.
Dígame su peso en kg: 73
Dígame su peso en kg: 
>>> datos
[56.0, 81.0, 73.0]
    
answered by 22.04.2018 / 02:14
source
5

In these cases, when dealing with a series of data, you can use a containing structure such as a List:

dato = []
cantidad = 10

for i in range(10):
  dato.append(float(input("Dígame su peso en kg: ")))

A list is a collection of possibly different types of data that can be accessed through an index. What we have done in the example is to request 10 weight values and save them in the order that we have entered them in a list type variable called dato . To access each value we use the index, for example:

print(dato[0]) # El primer elemento
print(dato[9]) # El último elemento

Some important considerations:

  • Remember that the index in every list starts at the 0 position, so the tenth element will correspond to the index 9 .
  • The creation of an empty list can be done using the "brackets", as in the example: dato = []
  • Like any object, the lists have different methods, one of the ones we are using is append() that allows you to add an element to the list and places it in the last position

For more information about lists:

answered by 22.04.2018 в 01:12