0

I am trying to write a code for a text based chat-bot as my first full scale project and I am having a weird problem with while loops that I can't figure out. My primary loop is dependent on text input, what is supposed to happen is when the user types "bye" the program ends. The only problem is when I run it, no matter what I say to it, it just prints out the same response over and over until I hit cntrl.+c or close the window. Here is the code: import datetime

print("Hi I am ChatBot V 0.0.1. I am here to converse. ")

user_name=raw_input("What is your name?")

print("Hello " + user_name + "!")

user_input=raw_input().lower().replace(" ","").replace("?","")

is_user_finished=0

while (is_user_finished == 0):

if user_input == "hi" or "hello":
    print("Hello. How are you?")

elif user_input == "whatisyourname":
    print("I do not have a name at the moment. What do you think I should be named?")
    user_input=raw_input().lower().replace(" ","").replace("?","")
    print("Good choice! I like that name!")

elif "where" and "you" in user_input:
    print("I was typed up by some kid in California")



else:
    print("I do not currently understand what you said. Sorry.")

if user_input == "bye":
    print("Good bye. It was nice talking to you!")
    is_user_finished = 1
6
  • 4
    Please indent the code correctly. Remember it's very important in Python. Commented Sep 1, 2014 at 3:58
  • 1
    Your code is not properly indented, and cannot run as such. Whitespace is significant in Python code. As such we can not verify your bug from this code. Commented Sep 1, 2014 at 3:58
  • As you are writing this for a learning purpose better start using Python3.x. There are lot of changes/improvements that have been done that will help you in future development. Commented Sep 1, 2014 at 4:34
  • 1
    Do not edit indentation here. Wrong indentation could be the actual cause of the problem. I rollbacked to original version. Commented Sep 1, 2014 at 5:48
  • Please double check your indentation! If it is how we see it now, you should be getting a "Expected indented block error" at your first if statement. Commented Sep 1, 2014 at 6:08

4 Answers 4

3

What happens is that the line

if user_input == "hi" or "hello":

don't do what you think. Instead, you should do

if user_input == "hi" or user_input == "hello":

or even, in a more compact way:

if user_input in ["hi","hello"]:

Something similar happens with the line

elif "where" and "you" in user_input:

which should be

elif "where" in user_input and "you" in user_input:
Sign up to request clarification or add additional context in comments.

Comments

0

syntax issue.

the correct form should be : if A == B or A == C, instead of A == B or C, which is incorrect.

Comments

0

You should change the line

if user_input == "hi" or "hello": to if user_input == "hi" or user_input == "hello":

and

"where" and "you" in user_input: to "where" in user_input and "you" in user_input:,
because "hello" and "where" is always True .

Besides you should put user_input=raw_input().lower().replace(" ","").replace("?","") into the while loop.

Comments

0

Provided you have proper indentation, which if the loop keeps running I assume you do, the problem, besides the things pointed out above about proper use of compound if statements is you need to add a "break" to the "bye" case.

if user_input == "bye":
    print("Good bye. It was nice talking to you!")
    is_user_finished = 1
    break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.