Python Forum
'answers 2' is not defined on line 27 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: 'answers 2' is not defined on line 27 (/thread-40656.html)



'answers 2' is not defined on line 27 - 0814uu - Sep-02-2023

Hey, I'm doing a school project and I can't find my error on replit, could someone help me and correct it? Go easy on me as I just picked up coding yesterday lol Blush

#Start of adventure
def intro():
  print("You encounter a stray cat on the sidewalk!")
  intro = input("1. Call the animal shelter, 2. Take her home, 3. Ignore her and keep walking: ")
  intro()
  #Animal shelter route
  if intro == "1":
    import random
    print(random.choice(answers1))
answers1 = ['They take her, start again for another ending!', 'The animal shelter asks you to adopt!', 'The cat follows you home!', 'The cat follows you home!']
if answers1 == 'The animal shelter asks you to adopt!':
  adopt = input("1. Accept", "2. Refuse", "3. Ask for compensation")
  if adopt == "1":
    pass
  if adopt == "2": 
    print("Oh well! You go home with no cat")
    pass
    if adopt == "3":
      answers3 = ['You receive money and go home happily!', 'The workers laugh at you and have you escorted out of the propery, start again for another ending!','The workers laugh at you and have you escorted out of the propery, start again for another ending!', 'The workers laugh at you and have you escorted out of the propery, start again for another ending!']
      import random
      print(random.choice(answers3))
      #Take home route
if intro == "2":
  answers2 = ['She escapes after a day!','You now have a lifelong companion!', 'Other cats start showing up to your front door!', 'Other cats start showing up to your front door!']
  import random
  print(random.choice(answers2))
if answers2 == 'She escapes after a day!':
  catescape = ['She is taken back to the animal shelter, start again for another ending!', 'You find her!', 'You find her!', 'You find her!']
  import random
  print(random.choice(catescape))
  



RE: 'answers 2' is not defined on line 27 - deanhystad - Sep-02-2023

answers2 is only defined if intro == 2.

Do you know what "pass" does? It appears in some odd places in your code.

Import modules once, at the top of the file.

I have doubts that your program will work. It is not structured like your typical adventure type game. Adventure games have a graph that moves you through the game. Each node in the graph presents you with multiple options, each option advancing you to a different node in the graph. It is often possible to return to the same node following a different path (making different choices). It is nearly impossible to program something like this using cascading if statements.


RE: 'answers 2' is not defined on line 27 - 0814uu - Sep-02-2023

(Sep-02-2023, 10:31 PM)deanhystad Wrote: answers2 is only defined if intro == 2.

Do you know what "pass" does? It appears in some odd places in your code.

Import modules once, at the top of the file.

I have doubts that your program will work. It is not structured like your typical adventure type game. Adventure games have a graph that moves you through the game. Each node in the graph presents you with multiple options, each option advancing you to a different node in the graph. It is nearly impossible to program something like this using cascading if statements.

I don't really know how pass works, it's just my teacher told me to write it as a placeholder. The project is only going to have 4 choices so that's why I'm just using if statements. Thank you for your help.


RE: 'answers 2' is not defined on line 27 - deanhystad - Sep-02-2023

Yes, pass is a placeholder for a block of code. That is what it is doing here:
  if adopt == "1":
    pass
Once you enter the block of code you remove the placeholder. Why is pass still here?
  if adopt == "2": 
    print("Oh well! You go home with no cat")
    pass
    if adopt == "3":
      answers3 = ['You receive money and go home happily!', 'The workers laugh at you and have you escorted out of the propery, start again for another ending!','The workers laugh at you and have you escorted out of the propery, start again for another ending!', 'The workers laugh at you and have you escorted out of the propery, start again for another ending!']
      import random
      print(random.choice(answers3))
      #Take home route
Since pass does nothing except act as a stand-in there is no harm leaving it there, but others looking at your code will think "That's odd." and look for meaning.

I drew out the graph to the best of my understanding. It is far more complicated than you think. Currently it is incomplete, but it looks like you should be able to return to the shelter and adopt the cat again forever and ever.

You are trying to solve this problem in the hardest way possible.


RE: 'answers 2' is not defined on line 27 - 0814uu - Sep-02-2023

(Sep-02-2023, 10:50 PM)deanhystad Wrote: Yes, pass is a placeholder for a block of code. That is what it is doing here:
  if adopt == "1":
    pass
Once you enter the block of code you remove the placeholder. Why is pass still here?
  if adopt == "2": 
    print("Oh well! You go home with no cat")
    pass
    if adopt == "3":
      answers3 = ['You receive money and go home happily!', 'The workers laugh at you and have you escorted out of the propery, start again for another ending!','The workers laugh at you and have you escorted out of the propery, start again for another ending!', 'The workers laugh at you and have you escorted out of the propery, start again for another ending!']
      import random
      print(random.choice(answers3))
      #Take home route
Since pass does nothing except act as a stand-in there is no harm leaving it there, but others looking at your code will think "That's odd." and look for meaning.

I drew out the graph to the best of my understanding. It is far more complicated than you think. Currently it is incomplete, but it looks like you should be able to return to the shelter and adopt the cat again forever and ever.

You are trying to solve this problem in the hardest way possible.

Thank you. This helps a lot.