Project Tanner

Portfolio

Python: Mad Lib Project

The below application is a simple Madlib game developed for Programming with Python, a class at FHSU. To run the application simply copy and paste into any Python supported IDE.

import os
import re
def madlib():
fileName = input('Enter the file name: ')
exist_fileName = os.path.abspath(fileName)

if os.path.exists(exist_fileName) == True:

        textFile = open(exist_fileName)
        fileText = textFile.read()
        textFile.close()
        print(fileText)

        text_regex = re.compile(r'ADJECTIVE|NOUN|VERB|ADVERB')
        match_text = text_regex.findall(fileText)

        for match in match_text:
                user_input = input('Enter ' + match + ': ')
                fileText = fileText.replace(match, user_input, 1)
        print(fileText)
        new_fileName = 'new_' + fileName
        new_file = open(new_fileName, 'w')
        new_file.write(fileText)
        new_file.close()

else:
        print('The file you have entered does not exist. Please enter a valid file name.')
        madlib()

madlib()