PYTHON BEGINNER Why are you giving the error 'is not defined' , how do I fix it?

Asked

Viewed 40 times

-4

Hello, I’m a beginner, I need help. I defined the populations of the regions:

popula_Nordeste = 27.09 popula_Sul = 14.26 populaca_Sudeste = 42.04 population = 8.82 population = 7.79

And I used an input , regiao = input("Type the region:").

So when I write popula_regiao appears that it is not defined. For example if I typed "South" in the input, the popula_regiao would have to be 14.26 wouldn’t it? I will leave an image to facilitate.inserir a descrição da imagem aqui

  • "I’ll leave an image to facilitate" - in fact the image makes things difficult. Ideally always put the code as text, understand the reasons reading the FAQ.

  • You have not created a variable called popula_regiao.

3 answers

1

Ai in your case, you did not start the variable popolucao_regiao nowhere. You will have to do a check by the value q the user typed. For example:

if regiao == 'sul':
   # aqui você pega o valor da região e divide pelo valor da populacao daquela região
   w = 15.2 / 14.26
elif regiao == 'nordeste':
   w = 21.5 / 27.09

In the way you did there, python cannot identify which region it should use to do the calculation

1


You have not created a variable called populacao_regiao. Also the type of your region variable is str, before doing the division operation make a conditional test so that you can use your created variables. Or use a dictionary. I suggest:

regiao = input('regiao: ')

regioes = {'norte':12.1, 'sul':14.7, 'nordeste':18.6, 'suldeste':21.8}
populacao_regiao = {'norte':25.4, 'sul':27.8, 'nordeste':29.1, 'suldeste':31.2}

regiao_populacao = populacao_regiao[regiao]
regiao = regioes[regiao]

w = regiao/regiao_populacao
""" ...Resto do seu codigo..."""

1

It’s just that you can’t actually access the variables "North", "South"... that way.

the calculation you are trying to make takes the value of your input which is a str and tries to split a variable which does not exist "populacao_regiao".

One way to do this calculation would be to use dictionaries:

regioes = {} 
regioes['Nordeste'] = 21.5
regioes['Sul'] = 15.2
regioes['Sudeste'] = 42.04
regioes['Norte'] = 8.82
regioes['Centro Oeste'] = 7.79

populacoes = {}
populacoes['Nordeste'] = 21.5
populacoes['Sul'] = 15.2
populacoes['Sudeste'] = 46.2
populacoes['Norte'] = 8.6
populacoes['Centro Oeste'] = 8.5


index = input("Digite qual região quer avaliar: Norte, Nordeste, Sul, Sudeste, Centro Oeste ")

w = regioes[index] / populacoes[index]

if w > 1:
  print(f'A região {index} está bem representada no ProUni')
else:
  print(f'A região {index} está bem representada no ProUni')

Browser other questions tagged

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