- Whitespace. It looks nice, and is important. Use it. There are many areas here where you could insert some whitespace, and then the code will magically become much easier to read. Here are a few areas where whitespace is needed.
- One blank line between the functions in
bot
. - Some more blank lines in between blocks of code in the module-level, and in any function in the
bot
class.
- One blank line between the functions in
- Secondly, the two variables near the top of the file,
startmes
, andhelpmes
, should be in one docstring, at the very top of the file, above the import statements. - Using
os.system("cls")
, oros.system("clear")
is not very portable, or cross-platform. At the moment, the most portable/cross-platform way to clear the screen would be this:os.system("cls" if os.name == "nt" else "clear")
. - Add some docstrings to these functions. Preferably, these docstrings should also be fleshed out with useful information on what these functions do, and how they do it.
- Most of your naming is okay. You do have some odd names like
maxwords
, orwordcount
. Preferably, variables with multiple words in their names, like these, should be named like this:max_words
,word_count
. In terms of how names are styled, functions and variables should besnake_case
, and classes should bePascalCase
. If the variable is constant it should beUPPERCASE_SNAKE_CASE
. - You don't need to include two parentheses
()
afterclass bot
. By default, you can write a class declaration like this,class MyClass:
, and by default, it will inherit fromobject
. Only use the parentheses if the class is inherited from another class, like this.class Enemy(Character):
. -
You mention in the
helpmes
variable, that the user shouldn't enter punctuation of any kind. This is sort of a hacky way to get user input, so I'd recommend usingstr.replace(item_to_replace, replace_with)
to remove punctuation. Here's an example that removes periods, commas, and apostrophes.user_input = raw_input("> ") .replace(".", "") .replace(",", "") .replace("'", "")
-
Finally if you ever implement a more advanced command system than just
#help
or#quit
, I'd recommend using a dictionary, rather than chainingif
/elif
/else
statements.
manpreet
Best Answer
2 years ago
I made a chat bot, that, as you talk to it, it learns to respond. But the way it speaks is strange, so if you have any ideas on how to make its response any more human, then please say so.
Anyway, you have to start a new chat bot. You'll notice that when you start his responses will be incredibly stupid. Once you talk to him enough, he gets more human, but not by much.
I am not asking for you to review features in the code, I am asking for the code and overall style to be reviewed.