-3

Hello overflow's community, today I was trying to create a project of a chat bot just in order to learn more about python, but I've faced a problem when I was creating a loop and didn't know how to solve it and tried to search the web and this website but i didn't find anything, here's my code:

l_greeting = ["hello", "Hello", "HELLO", "hELLO", "مرحبا"]
print ("Welcome to sami's chatting bot")
greeting = str(input("Feel free to chat the bot:  "))
    if greeting in l_greeting:
        print("Hello :D ")
    else:
        print ("I can't understand what you are saying, try again without 
using caps")
        break

and what I really want is making the code run from the point where the word is not in the list of my words and to make the script continue where it has left after that word.

Sam Ghanim
  • 17
  • 1
  • 1
  • 8
  • 2
    [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/953482) may be relevant to your interests. – Kevin Sep 29 '17 at 17:07
  • Thanks for it, but if someone could give me an example on my code it can be easier for me to understand because Im not very familiar with python – Sam Ghanim Sep 29 '17 at 17:15
  • 1
    I'm not really sure what you want to know, but one issue is that if you reach the `continue`, the control flow starts at the top of the loop again, so `print("Hello")` is never executed. – mkrieger1 Sep 29 '17 at 17:19

1 Answers1

1
  • You should put the input in your while loop to make user give input again and again.
  • Remove the continue before printing "Hello" in your if block
  • Remove the break as well.

Check the below updated code:

l_greeting = ["hello", "Hello", "HELLO", "hELLO", "مرحبا"]
print ("Welcome to sami's chatting bot")
while True:
    greeting = str(input("Feel free to chat the bot:  "))
    if greeting in l_greeting:
        print("Hello :D ")
    else:
        print ("I can't understand what you are saying, try again without using caps")
bhansa
  • 7,282
  • 3
  • 30
  • 55
  • line 5 if greeting in l_greeting: ^ IndentationError: unexpected indent – Sam Ghanim Sep 29 '17 at 17:24
  • I have created a snippet for you: https://repl.it/Lq15/0, and it works fine, check it out. – bhansa Sep 29 '17 at 17:28
  • Thank you, I've copied something wrong, but you've answered 50% of my question, now how can I make the script start from a place where the error comes rather than the begging, or it's the same as the above answer? – Sam Ghanim Sep 29 '17 at 17:31
  • I have did that too... check the example – bhansa Sep 29 '17 at 17:31