You are giving variable not defined within an if

Asked

Viewed 89 times

-3

Art. 42. Can adopt the over 18 (eighteen) years, regardless of marital status.

§ 1º Cannot adopt the ascendants and the brothers of the adopting.

§ 2º For joint adoption, it is indispensable that the adoptors are married civilly or maintain stable union, proven the stability of the family.

§ 3º The adopter must be at least sixteen years older than the one adopting him.

Art. 45. Adoption depends on the consent of the parents or the legal representative of the adopting.

§ 1. Consent will be waived in relation to the child or adolescent whose parents are unknown or have been deprived of family power.

§ 2º. In case of adopting older than twelve years of age, it will also be necessary your consent.

Write a program that captures the following information:

The Age of the Adopter

The adopter is brother or ascendant?

Is Joint Adoption?

Adopters are married or stable union?

Age of Adopting

Parents Unknown or Adopting Devoid of Family Power?

Consent of parents when not unknown?

Consent of the adopting (if older than twelve years of age)?

From the information provided, the program must inform whether or not it is possible to carry out the adoption!

I made the following code:

p1 = int(input("Digite a sua idade: "))

p2 = int(input("Digite a idade do adotando: "))

p3 = str(input("Você é irmão ou ascendente do adotando? [S/N]: "))

p4 = str(input("É adoção conjunta? [S/N]: "))

p5 = str(input("Os pais do adotando é desconhecido ou ele foi destituído do poder familiar? [S/N]: "))

if p1 < 18:

  print("Você não pode adotar!!")

elif p1 - p2 < 16:

  print("Você não pode adotar!!")

elif p3 == "S":

  print("Você não pode adotar!!")

elif p4 == "S":

  p6 = str(input("Vocês são casados ou possui união estável? [S/N]: "))

elif p6 == "N":

  print("Você não pode adotar!!")

elif p5 == "N":

  p7 = str(input("Os pais do adotando conscentiu a adoção? [S/N]: "))

elif p5 == "S" and p2 >= 12:

  p8 = str(input("O adotando conscentiu essa adoção? [S/N]: "))

elif p7 == "N":

  print("Você não pode adotar!!")

elif p8 == "N":

  print("Você não pode adotar!!")

else:

  print("PARABÉNS!! VOCÊ PODE ADOTAR.")

Gives the following error:


Digite a idade do adotando: 12

Você é irmão ou ascendente do adotando? [S/N]: N

É adoção conjunta? [S/N]: N

Os pais do adotando é desconhecido ou ele foi destituído do poder
familiar? [S/N]: S

---------------------------------------------------------------------------

NameError                                 Traceback (most recent call
last)

<ipython-input-3-64423a1d7de4> in <module>()

     12 elif p4 == "S":

     13   p6 = str(input("Vocês são casados ou possui união estável? [S/N]: "))

---> 14 elif p6 == "N":

     15   print("Você não pode adotar!!")

     16 elif p5 == "N":

NameError: name 'p6' is not defined ```
  • P6 only exists if P4 is equal S

  • 1

    Important whenever posting something, explain objectively and punctually the difficulty found, accompanied by a [mcve] problem instead of the whole exercise (inclusive, this helps to locate the error) - the excess information is undesirable and disturbs the focus of the post. To understand what kind of question serves the site and, consequently, avoid closures and negativities worth reading What is the Stack Overflow and the Stack Overflow Survival Guide (summarized) in Portuguese.

  • 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

I did not carefully analyze the statement so if the logic of the code is wrong I did not fix it. I only pressed to execute what is intended by looking only at the code solving the reported problem.

I took the function calls str() because it makes no sense to convert to string what is already string. I did not treat the error that will generate when the person type something that is not number where you expect a number, I leave it to you, here on the site is full of correct examples with regard to this using try except.

It is not so simple to make the logic when there are several paths to follow. You cannot follow a single block if because it will perform only one of these paths, and in case there are situations that some of them will be executed even if the previous ones are also.

In simple check situation you can make a single if to define non-adoption.

To facilitate the readability of the code I preferred to put in a function and when the person fails to adopt it terminates function. There are other ways to do it, but I find that one easier. Somete will show you can adopt when you don’t return before, so every time you warn you can’t adopt terminates the function.

The error is because you are trying to verify the value of a variable that does not exist, it will only exist if you enter the elif but if you enter the previous one no longer enters the error, the if is exclusive, only enters one of them. That’s why you need to ifs different and control the flow in some other way, as shown in the previous paragraph.

When a variable is only created within one if, use this variable can only happen within this if, can’t be another if, his existence is conditional.

Also I did not fix the fact that some cases type anything is the same as choose yes and there is case that no, this is inconsistent and should only accept the specific letter. Also I was wrong to accept both uppercase and lowercase.

I’m not sure I like the order of the questions. And the names of the variables could be more significant, it was difficult to understand the code with mnemonic names that I do not know and are not universal.

def adocao():
    p1 = int(input("Digite a sua idade: "))
    p2 = int(input("Digite a idade do adotando: "))
    p3 = input("Você é irmão ou ascendente do adotando? [S/N]: ")
    p4 = input("É adoção conjunta? [S/N]: ")
    p5 = input("Os pais do adotando é desconhecido ou ele foi destituído do poder familiar? [S/N]: ")
    if p1 < 18 or p1 - p2 < 16 or p3 == "S":
        print("Você não pode adotar!!")
        return
    if p4 == "S":
        p6 = input("Vocês são casados ou possui união estável? [S/N]: ")
        if p6 == "N":
            print("Você não pode adotar!!")
            return
    if p5 == "N":
        p7 = input("Os pais do adotando consentiu a adoção? [S/N]: ")
        if p7 == "N":
            print("Você não pode adotar!!")
            return
    if p5 == "S" and p2 >= 12:
        p8 = input("O adotando consentiu essa adoção? [S/N]: ")
        if p8 == "N":
            print("Você não pode adotar!!")
            return
    print("PARABÉNS!! VOCÊ PODE ADOTAR.")
    
adocao()

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • when adopting him is twelve years or older, he does not ask P8

  • That’s what you put in the code.

Browser other questions tagged

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