[PYTHON] Sane Libs

#Sane Libs
#Kevin Wong - kmw396
#CS164-069 / Lecture B

# introduction to program and user input for file
print('Welcome to a fun word replacement game.')
userFile = input('Enter the name of the file to use:\n')

# tests file and gracefully exits if the file is invalid
try:
    file = open(userFile,"r")
except:
    print('Error Bad File Name')
    exit()

# memory locations for output and vowels
output = ''
vowel = ['a','e','i','o','u']

# for loop for splitting the file 
for line in file:
    lineSplit = line.split()
    
    # for loop for checking each invidual word on each line
    for word in lineSplit:
        
        # if statement checks for words that start with [ 
        if (word[0] == '['): 
            
            # if statement checks for brackets with punctuation
            if word[len(word) - 2] == ']': 
                userInput = ''
                # displays the word inside of bracket. ex: [verb]. = verb and not verb].
                bracket = word[1: (len(word) - 2)] 
                # gets length of the word and subtract 1 to get the index for the punctuation
                punct = word[len(word) - 1] 
                
                #checks if the first letter is a vowel then decides which question to give
                if word[1] in vowel: 
                    userString = 'Please give an ' + bracket + '\n'
                    userInput = input(userString)
                else:
                    userString = 'Please give a ' + bracket + '\n'
                    userInput = input(userString)
                # gets all of the input, adds it together with punctuation and proper spacing.
                output += userInput + punct + ' '
   
            # if statement checks for brackets without punctuation
            elif word[len(word) - 1] == ']':
                userInput = ''
                # displays the word inside of the bracket. ex: [noun] = noun not noun]
                bracket = word[1: (len(word) - 1)]

                #checks if the first letter is a vowel then decides which question to give
                if word[1] in vowel:
                    userString = 'Please give an ' + bracket + '\n'
                    userInput = input(userString)
                else:
                    userString = 'Please give a ' + bracket + '\n'
                    userInput = input(userString)
                # gets all of the input, adds it together with proper spacing.    
                output += userInput + ' '
        
        # adds the rest of the story together
        else:
            output += word + ' '

# output
print('Here is your story:')
print('-------------------')
print(output)