My program seems not to be correct, what to improve?

Asked

Viewed 85 times

-2

maior50 = menor20 = maior10 = 0
x = 1

while x <= 20:
    idad = int(input("Digite sua idade:"))
    alt = float(input("Digite sua altura:"))
    peso = float(input("Digite seu peso:"))
    x = x + 1
    if idad >= 50:
         maior50 += 1
    if idad <= 20:
         menor20 += 1
    elif idad >= 10:
         maior10 += 1
    if idad < 40:
        medpeso = peso * 20 / 100

med = maior10 + menor20 / 2

print(f"Total de pessoas maiores de 50 Anos: {maior50}")
print("A média das alturas das pessoas com idade entre 10 e 20 anos é:%d"%med)
print("A porcentagem de pessoas com peso inferior a 40 quilos entre todas as pessoas é:%d"%medpeso)

Develop a program that reads the age, height and weight of 20 people. Validate data entry. This program should calculate and display:

  • a) The number of persons over the age of 50;
  • b) The average height of persons aged between 10 and 20;
  • c) The percentage of persons weighing less than 40 kilograms among all persons.

I wonder if the way I did is correct.

  • 1

    No, and I would recommend that you make one table test to understand what your code does.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

No, and it should be obvious.

The letter C asks to give the percentage, in no time you calculate percentage of anything, calculate something that I can not imagine what it is, can infer that is considering that the average is last weight input applying 20%. She says to consider people up to 40 kilos, for some reason you decided to consider people under 40 years of age. This is math and text interpretation and not programming.

The letter B says that the average should be people between an age range, in the case between 10 and 20 (does not speak if it is inclusive or exclusive), and did two different things, one for those who have up to 20, including those who have less than 10 and made another for people who have at least 10, including those over 20. And you’re still counting how many people you have, which is right, but where are you racking up the heights so you can do the average? And it goes without saying that the average calculation is wrong because it doesn’t even have the height data, it just has how many people fit the two meaningless criteria. Even more so if it is the average of people at that age then it should divide by the people counter and not by 2. Boolean logic study to create appropriate conditions to filter what enters the calculation and review how to calculate average (basic mathematics).

The letter A seems right, although the requirement is ambiguous, so there may be an error and not have to compare with greater or equal and be only the greatest, but there depends on how to question a text not very clear.

So it’s quite incorrect. It would be easy to identify this because doing it mathematically gives a very different result from programming. A code should do what you already know how to do on paper.

  • Good evening! I’m still a beginner and would like to know if there are mistakes in this change?

  • In fact you can not change the question, once asked the content should be the same, can edit to format, fix a spelling error, leave cleaner, but do not change the question. I reversed it. Ask another question.

Browser other questions tagged

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