I'm trying to make a french practice chatbot however users may not type the punctuation in the code. how do I fix this? this is my code. for example, I want to make it so that no matter what you type, like instead of "bonjour!" they can type "bonjour" only with no punctuation sensitivity. this may be not possible without a module but I don't care if it requires a module
#menu starts first
line1 = ("=-----------=")
line2 = ("-------------")
bots = ["Adam", "Marie", "James", "Joy"]
def conversation():
print("For your conversation, Choose a Bot")
print(line1)
for bot in bots:
print(bot)
print(line2)
botopt = input("option: ")
if botopt==("Adam").lower():
print("adam: Bonjour!")
msg = input("Type a message: ")
if msg==("Bonjour! Comment ca va?").lower():
print("adam: Très bien. et toi?")
msg = input("Type a message: ")
if msg==("Tres bien. Comment tu t'appelle?").lower():
print("adam: Je m'appelle Adam. Et toi?")
input("Name: ")
print("adam: Enchante. ")
def menu():
print("Bonjour! Please choose an area to practice on.")
wordoption = ["Verbs"]
placeoption = ["Restaurant", "Store", "Conversation"]
#pour le optionnes
print("Vocabulary")
print("=----------=")
for wopt in wordoption:
print(wopt)
print("=---------=")
print("Locations")
print("-----------")
for popt in placeoption:
print(popt)
print("--------------")
opt = input("option: ")
if opt==("Conversation").lower():
conversation()
menu()
i cant find info online so i am not sure what to do, so please try to help me, send me modules if needed
I'm also new to python, so if my suggestion(s) do not seem adequate to you, then don't use them. I use Python v3.10.2, and tested my solution in IDLE shell v3.10.2.
In the header portion of your code, you typed:
line1 = ("=-----------=")
and line2 = ("-------------")
I believe these are global variables, because these appear outside a defined function. You used these effectively in conversation(), your first function:
print(line1)
for bot in bots:
print(bot)
print(line2)
But, you did not use these in menu(), the second function. Why not? If for no other reason, I suggest you use it in the second function as well for consistency. This should improve your programming style, and make it easier for others to understand your code.
For the punctuation problem, I think you can probably work this out without the need of an additional module. If I understand the problem correctly, you don't want users to enter punctuation of any kind (! , . : ; - " ') in response to your code. Is this right? If so, you want users to be confined to using letters A-Z or a-z and nothing else in their response.
You could create another function that takes a single argument, such as:
def Response(msg):
# create an empty string for processed word with punctuation removed
rsp = ''
#msg = "Bonjour!"
for c in range(len(msg)):
word = msg[c] # index of 1st character in msg string = 0, 2nd = 1, 3rd = 2, etc. . .
# add exceptions here
if word != '!': # exclamation mark
rsp = rsp + word
elif word != ',': # comma
rsp = rsp + word
elif word != '.': # period
rsp = rsp + word
# etc...
else:
print('Please, do not use punctuation of any kind.')
print (word)
Keep in mind that this will work for single words input from the user. For full sentences, you'll have to allow for spaces to be used for word spacing.
Also, the French comment in the menu() definition should read:
pour les options
I'll try my best to clearly answer your question.
The Problem
First, your code uses capitalization in such a way that the capitalization of the input has to match that of the strings you're checking it with.
To fix that, I recommend making all your input string AND the input-checking strings lowercase and unpunctuated.
To do that, you can either chose to manually adjust all your checking strings, or you can just use str.lower().
To allow the user to only input unpunctuated and lowercase text, you can make your own function customInput() that works like input() but also removes all punctuation, and also lowercases the text before return.
Normal code
Here is the code, feel free to use/modify:
# you NEED to import this for the unpunctuation to work
from string import punctuation
# this is a function that works like input(), but removes all punctuation and makes the input lowercase
def customInput(text:str):
inputText = input(text).lower()
for character in [symbol for symbol in punctuation]:
inputText = inputText.replace(character,'')
return inputText
# do remember to use string.lower() on the strings you check the input with
Code for also removing numbers
If you want to remove numbers too from the input, then you can use this code:
# you NEED to import these for the unpunctuation to work
from string import punctuation, digits
# this is a function that works like input(), but removes all punctuation and numbers, and makes the input lowercase
def customInput(text:str):
inputText = input(text).lower()
for character in [symbol for symbol in punctuation] + [number for number in digits]:
inputText = inputText.replace(character,'')
return inputText
# do remember to use string.lower() on the strings you check the input with
Example usage
# an example of using customInput()
example = customInput('Saisissez une réponse: ')
# printing answer out
print(example)
# There will be no punctuation/capitalization in the printed output
Hope this helps! If it doesn't, comment and I will update the answer.
Related
I am trying to write a function that checks for a strong password. The password must contain one upper case, one lower case,a number and must be 8 characters long.
import re
def checker():
while True:
userInput = input(' Please input a password ')
passwordRegex = re.compile(r'[a-zA-Z0-9]+ {,8}')
match = passwordRegex.search(userInput)
if match:
print('Good!')
else:
print('Bad!')
checker()
This function always outputs Bad even when the password meets all the requirements. I have a feeling the error has to do with how I am using my Regex and Variables. I am using python 3.6.
Expanding on the answer from here:
passwordRegex = re.compile("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\S{8,}")
check out this demo
Using lookaheads we make sure there is at least one character from each group, then require at least 8 characters total. Note that you can customize the allowed characters (if you want to allow symbols) by changing the last group, the one before {8,}
Based on the feedback from #Aran-Fey and #Tomerikoo, i have updated my code and it works now.
import re
def checker():
while True:
userInput = input(' Please input a password ')
passwordRegex = re.search(r'^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$',userInput)
if passwordRegex:
break
print('Good!')
checker()
I'm trying to make a program deciding what determiner comes behind the input.
#For now, this only works with letters A-D.
MyInput = input("What word do you want to find out if the determiner is a/an?")
Finder = list(MyInput)
if ('a' in Finder):
print ("Start with an.")
else:
print ("Start with a.")
However, there is a little bug. When I input the word "bad", it says I need to have "an" go before the word. I want it to only say I need to have "an" be before the word when the first letter is A. What's the solution to this problem?
It is because in checks if the character can be found anywhere in the string. When you use "bad" it can. If you are checking the first character use Finder[0]
MyInput = input("What word do you want to find out if the determiner is a/an?")
Finder = list(MyInput)
if (Finder[0].lower() == 'a'):
print ("Start with an.")
else:
print ("Start with a.")
Regarding the question, using str.startwith would be the most clear way to go:
# For now, this only works with letters A-D.
my_input = input("What word do you want to find out if the determiner is a/an?")
if my_input.startswith('a'):
print ("Start with an.")
else:
print ("Start with a.")
Pay attention to the fact you do not need to make a list from a string: strings are iterable, they support in and indexing out of the box.
Also note the readability improvements:
Name variables using snake_case
Start a comment with a space
Do not put a conditional into brackets
See PEP8, the python style guide for more issues and details about codestyle.
Cheers!
I have an assignment which is to build a palindrome detector, and there is just one thing left in the code that I can't get my head around. I've been trying to fix it for days and now I need some assistance before I lose my mind...
The only thing left is for the program to remove unwanted characters from the users input, and replace it with nothing (""). So for example, the program is supposed to be able to interpret both "Anna" and "A!N!N!A" as palindromes. I'm required to use a for-loop to remove the characters.
> #the characters that need to be removed
not_valid = "?!\"\'#€%&/-()=? :,"
#user input
user_entry = tkinter.Entry(mid_frame, width = 67)
#variable with the user input, and transforms into lower case characters
text = user_entry.get()
text = text.lower()
So what I need is a for-loop that can help me to get the not_valid characters out of text. All the code I've been trying with so far Is useless. I would be really grateful for all the help I can get!
you can use regex module and sub function
import re
s = re.sub(r'[?!\"\'#€%&\-()=\s:,]', '', s)
s = re.sub(r'\W', '', s) # this will remove all non-alphanumerical chars
with for loop
for c in bad_chars:
s = s.replace(c, '')
For a more "simple" answer (although I personally think the first answer is simple enough) you can loop through each letter, then use the in keyword to check if that letter is one of the not_valid letters.
Here is an example:
text = user_entry.get()
text = text.lower()
not_valid = "?!\"\'#€%&/-()=? :,"
valid = "" #Create a new_variables were only the valid characters are stored
for char in text: #For every character in the text...
if char in not_valid: #If the character is in your not_valid list do not add it
continue
else: #Other wise add it
valid += char
print(valid)
I am trying to write a function that checks my input to see whether I have entered the character '?'.
This is what I got so far:
def check_word():
word = []
check = 0
user_input = input('Please enter a word that does not contain ?: ')
for token in user_input.split():
if token == '?':
print('Error')
check_word()
My input: hello?
It is supposed to show 'Error'. But it doesn't show anything. Could you please tell me what wrong it is in my code.
I would use the in operator to do this
def check_word(s):
if '?' in s:
print('Error')
For example
>>> check_word('foobar')
>>> check_word('foo?')
Error
The problem is how you split the string of the user_input.
user_input.split():
The example doesn't contain whitespaces so the condition isn't met. If you want for example to check a sentence with spaces, you should split it like this: user_input.split(' ') to split it on the spaces.
But for this example you have two choices:
1) You can just iterate over the input itself because you want to check every char in the string for whether it's a ?.
That is, change user_input.split(): into simply user_input without splitting. This option is good if you might ever want to add some sort of action for each char.
2) It's very easy just to use in, like this:
if '?' in s:
print('There is a question mark in the string')
This is a very simple solution that you can expand and check for other chars in the string as well.
It's because user_input.split() splits the user_input by whitespace. Since hello? does not contain any whitespaces, token is equal to your input and the loop is executed once.
You should iterate over user_input instead, or simply check if '?' in user_input.
Hey I am new I need some help with comparing strings
My Assignment is to make a chatbot, one that reads from a text file, that has possible things to input, and what the resulting output will be.
My problem is that it asks to choose the most suited one from the text file, easy yeh? but you also must save variables at the same time
Ok an example is one of the lines of the rules is:
you <w1> <w2> <w3> <w4> me | What makes you think I <w1> <w2> <w3> <w4> you?
You must save the <w1> and so on to a variable.
AND the input can be like, "did you know that you are really nice to me" so you have to adjust the code for that as well.
And also we cant make the code just for this text file, it is supposed to adjust to anything that is put into the text file.
Can someone help me ?
This is what I'm up to:
import string
import sys
import difflib
#File path:
rules = open("rules.txt", "rU")
#Set some var's:
currentField = 0
fieldEnd = 0
questions = []
responses = []
Input = ""
run = True
#Check if we are not at the end of the file:
for line in rules:
linem = line.split(" | ")
question = linem[0]
response = linem[1]
questions.append(question.replace("\n", ""))
responses.append(response.replace("\n", ""))
print questions
print responses
for q in questions:
qwords.appendq.split()
while run = True:
Input = raw_input('> ').capitalize()
for char in Input:
for quest in questions:
if char in quest:
n += 1
else:
if "<" in i:
n += 1
closestQuestion = questions.index(q)
print response
I would prefer pyparsing over any regex-based approach to tackle this task. It's easier to construct a readable parser even for more involved and complex grammars.
As a quick-and-stupid solution, parse input file and store entries in list. Each entry should contain dynamically-compiled "matching regex" (e.g. r'you (\w+) (\w+) (\w+) (\w+) me(?i)') and "replacement string" (e.g. r'What makes you think I \1 \2 \3 \4 you?'). For each incoming request, chat bot should match text agains regex list, find appropriate entry and then call regex.sub() for "replacement string".
But first of all, read some beginner's tutorial on Python. Your code is un-pythonic and just wrong in many ways.