PY Chatbot Repeating user input prompt - python

New to Python here. I've been coding a small chatbot for a while now, and when I prompt the user to enter something instead of answering with the correct response the bot just repeats the prompt. Everything before that works just fine, but it gets stuck in a loop of asking the user for an input.
print("Hello " + User_Name + """.
Tonic is a simple Python chatbot made in order to test things such as boolean logic
and variable definition.
(Also remember to speak to the bot in lowercase, without punctuation.)""")
while 1 == 1:
str1 = input("Say something: ")
#"hello" //////////////////////////////////////////
if "hello" in str1:
print("Hi " + User_Name + "! (I read this as the 'hello' greeting.");
#"hi" //////////////////////////////////////////
if "hi" in str1:
print("Hi " + User_Name + "! (I read this as the 'hi' greeting.");
#"how are you?" //////////////////////////////////////////
if "how are you" in str1:
print("good, how about you? (I read this as you asking 'how are you'.)")
mood = input("Enter Mood: ")
if "good" in mood:
print("Nice to hear " + User_Name + "! (I read this as you being in a good mood.)");
if "bad" in mood:
print("I hope you feel better soon, " + User_Name + "! (I read this as you being in a bad mood.)");
#"name length" //////////////////////////////////////////
if "name length" in str1:
print( "Your name is " + len(User_Name) + "letters long. (I read this as you asking how long your name is.");```

Your while loop does not have an exit condition. It will repeat forever since 1
is always equal to 1.
Instead, you could do while (str1 != "quit") which would stop the while loop once the user enters "quit" in the prompt.
Also, side note, you should not be using semicolons at the end of lines in Python.
print("Hello " + User_Name + """.
Tonic is a simple Python chatbot made in order to test things such as boolean logic
and variable definition.
(Also remember to speak to the bot in lowercase, without punctuation.)""")
while (str1 != "quit"): # You need an exit condition here
str1 = input("Say something: ")
#"hello" //////////////////////////////////////////
if "hello" in str1:
print("Hi " + User_Name + "! (I read this as the 'hello' greeting.")
#"hi" //////////////////////////////////////////
if "hi" in str1:
print("Hi " + User_Name + "! (I read this as the 'hi' greeting.")
#"how are you?" //////////////////////////////////////////
if "how are you" in str1:
print("good, how about you? (I read this as you asking 'how are you'.)")
mood = input("Enter Mood: ")
if "good" in mood:
print("Nice to hear " + User_Name + "! (I read this as you being in a good mood.)")
if "bad" in mood:
print("I hope you feel better soon, " + User_Name + "! (I read this as you being in a bad mood.)")
#"name length" //////////////////////////////////////////
if "name length" in str1:
print( "Your name is " + len(User_Name) + "letters long. (I read this as you asking how long your name is.")

Fix indentation
Convert output from len into string
print("Hello " + User_Name + """.
Tonic is a simple Python chatbot made in order to test things such as boolean logic
and variable definition.
(Also remember to speak to the bot in lowercase, without punctuation.)""")
while 1 == 1:
str1 = input("Say something: ")
#"hello" //////////////////////////////////////////
if "hello" in str1:
print("Hi " + User_Name + "! (I read this as the 'hello' greeting.");
#"hi" //////////////////////////////////////////
if "hi" in str1:
print("Hi " + User_Name + "! (I read this as the 'hi' greeting.");
#"how are you?" //////////////////////////////////////////
if "how are you" in str1:
print("good, how about you? (I read this as you asking 'how are you'.)")
mood = input("Enter Mood: ")
if "good" in mood:
print("Nice to hear " + User_Name + "! (I read this as you being in a good mood.)");
if "bad" in mood:
print("I hope you feel better soon, " + User_Name + "! (I read this as you being in a bad mood.)");
#"name length" //////////////////////////////////////////
if "name length" in str1:
print( "Your name is " + str(len(User_Name)) + "letters long. (I read this as you asking how long your name is.");

i just found a module which you can create a chatbot with just a handful lines of code
refer to this https://pypi.org/project/prsaw/
or
from prsaw import RandomStuff
random = RandomStuffV2()
str1 = input("message: ")
response = random.get_ai_response(str1)
print(response)

Related

Get Input from User then Use it in a Sentence

Hello everyone I need your help.
I am running this plain code on atom
name =input("What's your name? ")
print("Nice to meet you " + name + "!")
age = input("Your age? ")
print("So, you are already " + age + " years old, " + name + "!")
it is showing me the sand time icon at the bottom and doesn't give any output.
for other commands like
name = "Nick"
print(name)
is working perfectly.
I dont know what to do please help.
ptyhon version is 3.6.9

How to get user input? Python

I'm very new to this, so please be gentle!
As a little bit of work for my Python course, I am learning to run user input code. I put together the below code but when I run it using command-B, it asks me the 'What's your name?' question, but when I type in my name and click enter, nothing happens? For info, I am using Python 3.7, and using SublimeText.
I am 100% sure this is an easy answer, but surprisingly I cannot find the answer, and I have searched a little bit on here, and generally via Google, etc.
name=input("What's your name?:")
print("Hi",name,"how do you do.")
age=input("How old are you",name,"?:")
print("Great",name,"I'm",age,"years old too.")
city=input("Which city do you come from",name,"?:")
print("What a coincidence, I am from",city,"too.")
print(name,", here is your record //")
print(name, age, city)
Thanks for any help, and if you guys have an tips for a super newbie, it would be much appreciated!
You are having some issues because you have few syntax errors. The following is your code with the corrections. There you go:
name = input("What's your name?:")
print("Hi "+ name + " how do you do.")
age = input("How old are you " + name + " ?:")
print("Great " + name + "I'm " + age + " years old too.")
city = input("Which city do you come from " + name + " ?:")
print("What a coincidence, I am from " + city + " too.")
print(name + " , here is your record //: ")
print(name + " " + age + " " + city)
Good luck with learning Python :)
a = input() //for string
b = int(input()) //for int
c = float(input()) //for float
name=input("What's your name?:")
print("Hi"+name+"how do you do.")
age=input("How old are you "+name+" ?:")
print("Great "+name," I'm ",age+" years old too.")
city=input("Which city do you come from "+name+" ?:")
print("What a coincidence, I am from "+city+" too.")
print(name+", here is your record //")
print(name+age+city)
Use + for concatenation inside print

How to make a program which will tell the user how many letters were in their surname or firstname

How to make a program which will tell the user how many letters were in their surname or first name. Here I have the code so maybe you have any adjustments to it just let me know. Thanks:)
print( len( input("what is your name? ") ) )
Unfortunately, your code is in an image. I use a screen reader so can't see the image, but I can write an example program:
#Example 1, count all characters in the user's input
fn=input("What is your first name?")
ln=input("What is your surname?")
print("Your first name has " + str(len(fn)) + " characters, and your surname has " + str(len(ln)) + " characters.")
#Example 2, count *only* ASCII letters in user's input
import string
fn=input("What is your first name?")
ln=input("What is your surname?")
fncount=0
lncount=0
for i in fn:
if i in string.ascii_letters:
fncount+=1
for i in ln:
if i in string.ascii_letters:
lncount+=1
print("Your first name has " + str(fncount) + " letters, and your surname has " + str(lncount) + " letters.")

Python: Loop Prompt Back Around

A lot of the errors of this Python file have been fixed but there is one last thing it's not working. I need the else statement to loop back to ask the question again if neither Yes or No is entered. You can probably see by the code what I was going for but I'm probably not even on the right track. Can someone help me with this one last thing?
#These are my variables which are just strings entered by the user.
friend = raw_input("Who is your friend? ")
my_name = raw_input("Enter Your Name: ")
game_system = raw_input("What's your favorite game system? ")
game_name = raw_input("What's your favorite game for that system? ")
game_status = raw_input("Do you have the game? (Yes or No) ")
game_store = raw_input("What is your favorite game store? ")
game_price = raw_input("What's the price of the game today? Enter a whole number. ")
#This is what is printed after all the prompts. There is no condition for this print.
print "I went outside today and my friend " + friend + " was outside waiting for me. He said \"" + my_name + ", did you get the new " + game_system + " game yet? You were getting " + game_name + " today, right?\""
#If the condition on the Yes or No question is yes, then this code runs.
if game_status == "YES":
print "\"You know I got it, man!\" I said. \"Awesome! Let's play it,\" he said. \"I heard " + game_name + " is supposed to be amazing!\" We went back inside and took turns playing until " + friend + " had to go home. Today was a fun day."
#If the condition is No, then this code runs.
elif game_status == "No":
print "\"Well let's go get it today and we can come back here and play it!\" We went down to " + game_store + " and baught " + game_name + " for $" + str(game_price) + " and we went back to my house to take turns playing until " + friend + " went home. Today was a good day. (Now try again with the No option!)"
#If the condition meets neither Yes or No, then this code runs, sending the user back to the same question again. This repeats until a condition is met.
else:
raw_input("That answer didn't work. Try again? Do you have the game? (Yes or No) ")
This:
if game_status: "YES"
isn't how you make an if statement. You're treating it like the syntax is
if some_variable: some_value
and if the variable has that value, the if statement triggers. In fact, the syntax is
if some_expression:
and if the expression evaluates to something considered true, the if statement triggers. When you want the if statement to trigger on game_status equalling "YES", the expression should be game_status == "YES", so the if line should go
if game_status == "YES":
Similarly, the elif line should go
elif game_status == "NO":
I would encourage to you always break to a new line after a conditional...
if game_status == "YES":
print "\"You know I got it, man!\" I said. \"Awesome! Let's play it,\" he said. \"I heard " + game_name + " is supposed to be amazing!\" We went back inside and took turns playing until " + friend + " had to go home. Today was a fun day."
anything that is indented after the "if game_status:" will get run. And it reads better.
edit:: if you use single quotes for all strings then you don't need to escape the double quotes...
print '"You know I got it, man!" I said. "Awesome! Let\'s play it," he said. "I heard ' + game_name + ' is supposed to be amazing!" We went back inside and took turns playing until ' + friend + ' had to go home. Today was a fun day.'
it's a matter of preference...but may look less cluttered.

Python error: cannot concatenate 'str' and 'builtin_function_or_method' objects

I am currently in the process of programming a text-based adventure in Python as a learning exercise. So far, the player can name themselves, the value of which is stored in a dictionary key. However, when I attempt to allow the player to choose their race, I get the following error:
cannot concatenate 'str' and 'builtin_function_or_method' objects
I have gone over my code time and time again and can't seem to figure out what's wrong. I'm somewhat new to Python, so I assume it's something simple I'm overlooking.
player = {
"name": "",
"gender": "",
"race": "",
"class": "",
"HP": 10,
}
def error():
print "Error: Unknown Command"
print "You will have to forgive me, " + player['name'] + ". My eyesight isn't what it used to be. What are you, exactly?."
print "- A mighty HUMAN"
print "- A hardy DWARF"
print "- An ingenious GNOME "
print "- or an elegant ELF"
print "(Hint: If you would like to know more about each race, consult the manual, whatever that means)"
player_race = raw_input(">> ").lower
while race_confirm == False:
if player_race != "elf":
print "You say you're a " + player_race + ". Is that correct? Remember, you will be unable to change your race later. (Y/N)"
response = raw_input(">> ").lower()
else:
print "You say you're an " + player_race + ". Is that correct? Remember, you will be unable to change your race later. (Y/N)"
response = raw_input(">> ").lower()
if response == "y":
player_race = player['race']
print "It is nice to meet you, ", player['name'] + "the" + player['race'] + "."
race_confirm = True
elif response == "n":
print "Oh, I'm terribly sorry. I must have misheard you. What did you say you were again?"
player_name = raw_input(">> ")
else:
error()
You need to call that lower method, it's a callable attribute:
player_race = raw_input(">> ").lower()
# ^^

Categories