I've done the following exercise from automating the boring stuff with Python Chapter 8:
Create a Mad Libs program that reads in text files and allows the user
Insert your own text anywhere the word ADJECTIVE, NOUN, ADVERB or VERB
appears in the text file. For example, a text file might look like this:
The ADJECTIVE Panda went to the NOUN and then VERB. A nearby NOUN
was not affected by these events. The program would find this
Occurrence and ask the user to replace them.
Enter an adjective:
Enter a noun:
Enter a verb:
Enter a noun:
The following text file would then
to be created:
The stupid panda went to the chandelier and then screamed. A nearby
The pickup was not affected by these events. The results should be
printed on the screen and saved in a new text file.
I decided to read from all text files that are in the same folder as the Python script.
All newly created files will end with _mad.txt
I would like to know if this is a good solution.
Are there bad practices?
Is the code easy to understand?
What can be improved?
Are there better approaches to code for some parts?
Please do not hesitate to comment on anything you can find.
Here is the code:
Searches for all TXT files in the working folder.
When a file is found, the file is searched for the keywords
Subsequently, the user is prompted to add a replacement word for the keyword.
A file with the name _mad.txt is created in the folder.
In this file, the keywords are replaced by the user input
def filenames_in_script_folder ():
Returns all filenames that are in the same folder
like this running Python script
os.chdir (os.path.dirname (sys.argv))
return os.listdir (os.getcwd ())
def words_from_file (filename):
Reads a text file and returns all its words
File = open (file name)
file_content = file.read ()
return file_content.split ()
def ask_for_replace_word (keyword):
Ask for a replacement for the mentioned keyword.
Checks if the keyword is a vowel to follow the correct English grammar
vowel_regex = re.compile (& # 39; ^[aeiou]& # 39)
If vowel_regex.search (keyword):
Return input ("Enter" + keyword.under () + " n")
Return input ("Enter a" + Keyword.lower () + " n")
def replace_word (word, keywords):
Replaces the specified word if it matches one of the keywords.
Non-alphabetic characters are ignored during keyword comparison
Otherwise, the specified word will be returned
no_char_regex = re.compile (& # 39;[^a-zA-Z]& # 39)
clean_word = no_char_regex.sub (& # 39;, word)
for keywords in keywords:
If clean_word == keyword:
new_word = ask_for_replace_word (keyword)
return word.replace (keyword, new_word)
Send word back
def write_data_to_file (data, file name):
Writes the provided data to the file.
If there is no file, a new one will be created first
File = open (filename, & # 39; w & # 39;)
def mad_libs ():
Main function reads from file, replaces keywords
and write in new file
by filename in filename_in_script_folder ():
if filename.lower (). endswith (& # 39; txt & # 39;):
new_words = 
replace_a_word = False
for a word in words_from_file (filename):
KEYWORDS = (& # 39; ADJECTIVE & # 39 ;, & # 39; NOUN & # 39 ;, & # 39; VERB & # 39 ;, & # 39; ADVERB & # 39;
replace = replace_word (word, KEYWORDS)
if replace! = Word:
replace_a_word = true
new_data = & # 39; & # 39; .join (new_words)
write_data_to_file (new_data, filename)[:-4] + "_mad.txt")
I pass Pylint over the code and in the definition of the keywords in the mad_libs function, it gives me:
Severity Code Description Status of the suppression of the project file line
The name of the message variable "KEYWORDS" does not match the naming of snake_case
Should not this ALLCAPS be like me because those words should be constants?