How to make Python read code differently. Help

Hello Programmers. It is I, a mere beginner. I require assistance.

#introduction to I.A.N 1.0

print ("Hello, I am I.A.N, your personal Artificial companion! What should I call you?")
name = input()

#Naming

if name == "Ian":
    print ("Okay, I will now call you", name,"... wait, we have the same name! How cool is that!")
else:
    print ("Okay, I will now call you", name,)
    
print ("Do you want me to call you by a title?")
yesno = input()
if yesno.casefold() == "yes":
    print ("As you wish, what will this title be?")
    title = input()
    print ("I will now call you by the title of", title,)
else:
    print ("As you wish")
    title = None 

#Questions/responses          oh hi.

question = input()

if question == "How are you":                                                          
    print ("I am great thank you! Might I ask how are you? Are you Happy or Sad?")
    answer = input() 
if answer == "Happy":
    print ("I am glad to hear that,", name or title,)
    input()
if answer == "Sad":
    print ("Oh, I hope you feel better soon,", title or name,)
    
question = input()

if question == "Are you real":
    print ("Sadly not, but you can still talk to me somehow")
    input()

So. I am making a Chatbot, as you do, and have recently fixed a bug regarding the question “Are you real,” not printing as it should. However, I have now come across a new bug. :slight_smile:
When I run the program and answer the naming stuff at the top of my script and immediately ask “Are you real,” it throws up an error regarding my ‘Answer’ variable:

Traceback (most recent call last):
  File "C:\Users\andre\AppData\Local\Programs\Python\Python311\I.A.N.py", line 30, in <module>
    if answer == "Happy":
NameError: name 'answer' is not defined

I assume Its giving me this error as in the code for the “Are you real,” I have not mentioned an ‘Answer’ Variable and It wants one. Unfortunately.
I think it’s reading the code top to bottom and as i have used the Variable ‘Question’ again for “Are you real,” It gets confused and tries to run the code for “How are you,”

I would much appreciate any help with this as if it can be fixed, my Chatbot will almost be ready to add more things and features to.

Thank you, Andrew Rowland

The problem is that answer is only defined when if question == "How are you":. So if this doesn’t happen, answer never gets defined. You could get around this by initializing answer as something like answer = '', so that it is always defined as something.

2 Likes

Or indent the code that uses answer to the same level as the input().

1 Like

Yep, placing all references to answer under the initial condition also works.

1 Like

Thanks! :grin: :grin: :grin:

Right. New bug.
Its fixed the answer error (Thank you! :grin:) but it has stopped printing “are you real” properly and only works if I do “How are you” first and give the answer of “Happy” (and it HAS to be Sad) and THEN ask “Are you real”. If I ask “Are you real” first it does nothing and doesn’t even try to print anything. :pensive:

Hello, I am I.A.N, your personal Artificial companion! What should I call you?
Andrew
Okay, I will now call you Andrew
Do you want me to call you by a title?
no
As you wish
Are you real

Here is an example of when i ask it in the order that works:

Hello, I am I.A.N, your personal Artificial companion! What should I call you?
Andrew
Okay, I will now call you Andrew
Do you want me to call you by a title?
no
As you wish
How are you
I am great thank you! Might I ask how are you? Are you Happy or Sad?
Sad
Oh, I hope you feel better soon, Andrew
Are you real
Sadly not, but you can still talk to me somehow

And this is an example of when I put “Happy” not “Sad”:

Hello, I am I.A.N, your personal Artificial companion! What should I call you?
Andrew
Okay, I will now call you Andrew
Do you want me to call you by a title?
no
As you wish
How are you
I am great thank you! Might I ask how are you? Are you Happy or Sad?
Happy
I am glad to hear that, Andrew
Are you real

I am finding this oh so very confusing and more help would be appreciated!

Thank you, Andrew Rowland :confounded:

I’m not entirely sure what you did to fix things. I know multiple suggestions were made, and I also don’t know if you implemented one of them exactly as intended. Seeing your new code would be helpful.

Here is my current code

answer = ''

#introduction to I.A.N 1.0

print ("Hello, I am I.A.N, your personal Artificial companion! What should I call you?")
name = input()

#Naming

if name == "Ian":
    print ("Okay, I will now call you", name,"... wait, we have the same name! How cool is that!")
else:
    print ("Okay, I will now call you", name,)
    
print ("Do you want me to call you by a title?")
yesno = input()
if yesno.casefold() == "yes":
    print ("As you wish, what will this title be?")
    title = input()
    print ("I will now call you by the title of", title,)
else:
    print ("As you wish")
    title = None 

#Questions/responses          oh hi.

question = input()

if question == "How are you":                                                          
    print ("I am great thank you! Might I ask how are you? Are you Happy or Sad?")
    answer = input() 
if answer == "Happy":
    print ("I am glad to hear that,", name or title,)
    input()
if answer == "Sad":
    print ("Oh, I hope you feel better soon,", title or name,)

question = input()

if question == "Are you real":
    print ("Sadly not, but you can still talk to me somehow")
    input()

Here is my guess. Here you have an input that you do not assign to a variable:

if answer == "Happy":
    print ("I am glad to hear that,", name or title,)
    input() # Why is this here?

I imagine you are typing your question here and then pressing enter again because you’ve already typed the question. So then it never gets the real question.

Remove any input() that you do not set to a variable as they are doing nothing but confusing you.