How to stop infinite loop in simple chatbot [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a simple chatbot with the following code
import random
human_input = input('Talk to me: ')
continue_dialogue = True
greeting_inputs = ("hey", "good morning", "good evening", "morning", "evening", "hi", "whatsupp")
greeting_responses = ["hey", "hey hows you?", "*nods*", "hello, how you doing", "hello", "Welcome, I am good and you"]
def generate_greeting_response(input):
for token in input.split():
if token.lower() in greeting_inputs:
return random.choice(greeting_responses)
while continue_dialogue:
for token in human_input.split():
if generate_greeting_response(human_input) is not None:
print("Chatterbot: " + generate_greeting_response(human_input))
input('Talk to me again: ')
else:
print("Chatterbot: Bye")
continue_dialogue = False
What I wanted was that if human_input is recognized inside greeting_inputs for the conversation to keep going and if it isn't, then the conversation would stop. But the code above never stops the conversation even if I input nonsense. Why is my else statement never activating?

Update input('Talk to me again: ') to
human_input=input('Talk to me again: ')
Since next time it asks, you haven't given it a variable to assign the input, it keeps repeating

Related

Working on a python project and there are syntax errors never faced before [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
def main_menu():
print("WELCOME TO LIBRARY")
print("What would you like to do?")
option=print(input("1. New user \n 2. Existing user ")
if option == 1:
new_user()
elif option == 2:
old_user()
else:
print("please choose a correct value")
main_menu()
def new_user():
print("new user")
def old_user():
print("Old user")
main_menu()
Here is a picture of the error, it says syntax error missing ")" for function call and missing "else" for the first one at "if":
the line
option=print(input("1. New user \n 2. Existing user ")
is missing a closing )
it should be
option=print(input("1. New user \n 2. Existing user "))

Python line not running [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
while True:
inp = raw_input()
if inp == "":pres= input("What would you like to know about me? AGE, JOKE, FACT")
if pres in {'AGE'}:
print("I was birthed from my mother 87 years ago.")
if pres in {'JOKE'}:
print("Where do polar bears keep their money?")
import time
time.sleep(2)
print("In a snow bank!")
if pres in {"FACT"}:
print("Hippopotamus's have pink spit!")
I am a student and for my school project have to do a code and for some reason I can't figure out how to get the chatbot to answer the question when someone puts in AGE, JOKE or FACT. Insteas, it just repeats the question when I press enter.
You are asking for input in your while True loop. It will never exit, because True is... always true. You need to put the if statements inside the loop for them to execute as well.

Python is stopping before running a loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm having trouble with this code. It will run normally until it gets to the loop (def Game) and just stops there. I did have it as an if statement but that still didn't work. Please note that this may be a bit messy.
import random
import time
GameInProgress = ("Yes")
TutorialDone = ("No")
ReplayGame = ("Yes")
#Test purposes
PlayerName = ("Lewis")
print ("Welcome to 'Guess The Word!")
def Game():
GameInProgress = ("Yes")
TutorialDone = ("No")
ReplayGame = ("Yes")
#Test purposes
PlayerName = ("Lewis")
print ("Welcome to 'Guess The Word!")
WordSelected=("No")
LettersGuessed=0
print (TutorialDone)
EnterName = input("Would you like to enter your name?").title()
def Game(): is not a loop, it is a function it does not execute until you call it.
you can call a python function in this way
Game()
if you want to call the same function again and again simply you can call the function inside a for or while loop:
while(condition):
Game()
if your are very beginner follow some tutorials
https://www.tutorialspoint.com/python/python_functions.htm

Python - How do I get this to read the code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Here is my code
import time
print("------------------------------------------")
print(" Welcome to Senpai Quest")
print('------------------------------------------')
time.sleep(3)
print("")
print('You have English next, there is a test. If you hit yes .you go to
class and you get +1 charisma and Knowledge.')
print("")
print('If you hit no, you will skip class and get +1 Courage and +1 risk.')
ans1=str(input('Do you Take the english test? [Y/N]'))
if ans1== ['y']:
print("It works")
else:
print("Woo Hoo!")
When it asks the question and for the 'y' it just goes straight through to "woo hoo!". What i would like it to do is to print "It works" but if you type n it just goes to woo hoo. Please help
This should work:
ans1 = input('Do you Take the english test? [Y/N]').strip()
if ans1.lower() == 'y':
print("It works")
else:
print("Woo Hoo!")
I suggest using the distutil's strtobool in order to cover all cases
from distutils.util import strtobool
ans1=input('Do you Take the english test? [Y/N] ').strip()
if strtobool(ans1):
print("It works")
else:
print("Woo Hoo!")

Python programming error, elif statment syntax error Simple code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
name=input("Hello person, Whats your name?")
print("Hello", name)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
print("Ok", name,", listen up")
print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
choice2=input("What do you want to call the house?")
print("The old,",choice2,"was once owned by an old lady. ")
elif choice==("maybe"):
print("You found an easter egg, congrats. PS this does nothing")
Whats wrong with this code?? It says in the idle shell syntax error. The last elif statement isn't working.
This is a petty indentation issue, your print statements for the if blocks are not indented right and so the elif seems to be out of place. Note that python keeps track of logical blocks by the indentation.
name=input("Hello person, Whats your name?")
print("Hello", name)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
print("Ok", name,", listen up")
print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
choice2=input("What do you want to call the house?")
print("The old,",choice2,"was once owned by an old lady. ")
elif choice==("maybe"):
print("You found an easter egg, congrats. PS this does nothing")
As already pointed out, if choice==("yes" or "yes " or "Yes" or "Yes ") is wrong, use if choice.lower().strip() == "yes" instead, or if choice in ("yes", "yes ", "Yes", "Yes ").
If in case this is python 2, input will throw an error, use raw_input instead.
Also print with multiple statements will throw errors as well if used like a function, so change them from print(statement_x, statement_y, statement_z) to print statement_x, statement_y, statement_z

Categories

Resources