How would you then declare a loop with while, remove elements from it?

0

How could in this case draw the ages that are less than 18, those that are greater than 18 and then print them? I did it that way but the loop works but not the print.

Edad_persona=int(input("Ingrese su edad:"))
while Edad_persona!=999:
    print("Ingrese otra edad")
    Edad_persona=int(input("Ingrese su edad:"))
if Edad_persona<18:
    print("La cantidad de personas menores a 18 años son" ,Edad_persona)
if Edad_persona>=18:
    print("La cantidad de personas mayores a 18 años son" ,Edad_persona)
print("Programa terminado")

Edit (Moved here from a comment)

In that case, if I want to add the younger and older ages and then draw an average, I try this way but it does not give me ...

Edad_persona=int(input("Ingrese su edad:"))
menores=1
mayores=0
Menores_promedio=0
Mayores_promedio=0
while Edad_persona!=999: 
    Edad_persona=int(input("Ingrese su edad:"))
if Edad_persona<18: 
    menores=menores+1 
if Edad_persona>18: 
    mayores=mayores+1 
Menores_promedio=(Menores_promedio+(Edad_persona<18))
Mayores_promedio=(Mayores_promedio+(Edad_persona>18))
print("Los menores a 18 son:" ,menores) 
print("Los mayores a 18 son:" ,mayores) 
print("El promedio de los menores es de:" ,((Menores_promedio)/menores)) 
    
asked by Ivan 07.04.2018 в 19:46
source

1 answer

0

The code you provide with your attempt to calculate the number of adults and minors, has major problems that denote perhaps too hasty an attempt to start programming without understanding the bases.

I will try to help you with this question, but you should seriously consider following a good course or introductory book to understand the basic concepts about loops and conditionals.

These are the problems that your code presents:

  • The while loop in your code will be repeated while the user enters ages other than 999, but each new age entered is always saved again in the same variable, thus losing the previous entries. Once you exit that loop, the variable Edad_persona will have only the last entered age.
  • To partially fix that problem, the code that looks if the age is greater or less than 18 should also be part of the loop.
  • The line Menores_promedio=(Menores_promedio+(Edad_persona<18)) does not work as you think. Edad_persona<18 is a "boolean", that is, a value that can only be True or False . Therefore, you are adding a boolean to Menores_promedio , instead of adding the age. I understand that maybe you wanted to do "add the age if it is less than 18", but precisely for these things are the if .
  • The above problem is solved by entering the line that updates Menores_promedio in the if that checked if the age was less than 18.
  • What if the age is 18? That case you're not telling. Since 18 years old is of age, I understand that it should be counted in the case >=18 .
  • The names of the variables are not appropriate, since Menores_promedio is not the average (at the moment) but only the sum, so it would be better to call suma_menores . In addition, following the typical recommendations, variable names should not start with a capital letter.
  • It is convenient to comment on what the program does. It will help you to understand it.

Putting all these ideas into practice, the code will look like this:

# Inicializamos con cero contadores y sumas
n_menores=0
n_mayores=0
suma_menores=0
suma_mayores=0

# Leemos la primera edad
edad_persona=int(input("Ingrese su edad:"))
while edad_persona != 999:
    # Si la edad es != 999 la procesamos
    if edad_persona < 18:
        n_menores = n_menores+1 
        suma_menores = suma_menores + edad_persona
    if edad_persona >= 18: 
        n_mayores = n_mayores+1 
        suma_mayores = suma_mayores + edad_persona
    # Leemos la edad de la siguiente (y con este nuevo valor
    # se repetirá todo el while)
    edad_persona=int(input("Ingrese su edad:"))

# Hemos salido del while, tenemos los resultados
print("Los menores a 18 son:", n_menores) 
print("Los mayores a 18 son:", n_mayores) 
print("El promedio de los menores es de:" ,((suma_menores)/n_menores)) 
    
answered by 08.04.2018 в 19:53