Python Forum
'answers 2' is not defined on line 27
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'answers 2' is not defined on line 27
#1
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))
  
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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.
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Division calcuation with answers to 1decimal place. sik 3 2,138 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
  Cannot Assign right Answers To Shuffled Questions Boblows 6 2,776 Jan-22-2021, 09:41 AM
Last Post: buran
  python library not defined in user defined function johnEmScott 2 3,885 May-30-2020, 04:14 AM
Last Post: DT2000
  Using answers from an input function Totalartist 3 2,212 Jul-19-2019, 02:02 PM
Last Post: snippsat
  classes , error at line 88 name finnKategori not defined sydlender 3 2,949 Nov-12-2018, 09:08 PM
Last Post: buran
  Writing Answers From Questionnaire to a new .txt Document FizzyBreak579 4 3,576 Feb-16-2018, 07:26 AM
Last Post: buran
  Need Answers 20nick20 6 5,434 Apr-29-2017, 04:06 PM
Last Post: Larz60+
  Code that generates MD5 hashes from IPv6 addresses giving differant answers? PyMD5 4 6,476 Oct-17-2016, 02:39 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020