Help with my Chatbot, I.A.N (I'm new to Python!)

Hello, I am making a Chatbot called Ian and need help with his question and response code. At the moment you can ask him the question how are you with no problem. If you look there would be a maximum of 4 lines of string when its being run. However, when I run him and ask Are you real I have to click enter 4 times before his answer shows up. It is leaving the lines where the other question could have been even if I didn’t ask him it.

I would like it to be straight after/below which is how it is for the question How are you. If any of you more experienced programmers help me out it would be much appreciated. Ah, but that’s not all…

If I ask the question after I have asked How are you, It does not work. I presume it is trying to print it where it was before but now that text is there it doesn’t print. Please help.

Finally, How can I make the questions repeatable? As of current, you can only ask each question once as well as the other bugs I described earlier. I need help.

Thank you,
Andrew Rowland, A very much confused beginner programmer

Here is one image of my problems.

And the other.

Post code as text, not screenshots. Three backticks before and after will preserve formatting. I’m not going to stare at images to try to figure out what your code is doing.

1 Like

Please wrap code in triple backticks to preserve the formatting:

```python
if True:
    print(''Hello world!')
```

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 == "yes" or "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,)
if answer == "Sad":
    print ("Oh, I hope you feel better soon,", title or name,)
input()

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

The line:

if yesno == "yes" or "Yes":

is evaluated as (yesno == "yes") or "Yes" and non-empty strings are always treated as true.

That line should be:

if yesno == "yes" or yesno == "Yes":

or, better yet:

if yesno.casefold() == "yes":

The .casefold method will return a string with any uppercase letters converted to lowercase.

Later on you’re waiting for input:

question = input()

without printing any prompt for it, and if the question is “How are you” you’re printing a message, and then waiting for input again:

answer = input()

unconditionally (it’s not indented).

1 Like

might want to consider a pre trained transformer model for a conversation system,

from sentence_transformers import SentenceTransformer, util
model = SentenceTransformer('msmarco-MiniLM-L-6-v3')

query_embedding = model.encode('I feel happy')
passage_embeddings = model.encode(['As you wish', 'I am glad to hear that', 'Oh, I hope you feel better soon'])

print('similarity:', util.cos_sim(query_embedding, passage_embeddings))

similarity: tensor([[0.3474, 0.4809, 0.4610]])

the one with the highest cosine similarity would be your response, here, ‘I am glad to hear that’