I am fairly new to python and I am trying to figure out why my functions aren't giving me the output I am looking for. I feel like I have tried everything and tried researching, but I haven't found anything that has helped me.
Here is my code:
from sys import argv
def GetCharactersWords(input):
characters = input.read()
words = len(characters.split())
#word = len(words)
return words
def GetCharacterNumber(input):
characters = input.read()
number = characters.count(".")
return number
def FirstSentence(input):
characters = input.read()
fields = characters.split(".")
field1 = fields[0]
return field1
def LengthFirstSentence(input):
characters = input.read()
fields = characters.split(".")
field1 = fields[0]
field0 = len(field1)
return field0
def LastSentence(input):
characters = input.read()
fields = characters.split(".")
field2 = fields[-1]
return field2
def LengthLastSentence(input):
characters = input.read()
fields = characters.split(".")
field3 = fields[-1]
field4 = len(field3)
return field4
def print_to_file(word_characters, number_characters, first_sentence, length_first,
last_sentence, length_last):
with open("output.txt", "w") as outfile:
outfile.write("Here is the text file: " + '\n')
outfile.write(str(input) + '\n')
outfile.write("Number of words in the file?" + '\n')
outfile.write(str(word_characters) + '\n')
outfile.write("Number of sentences in the file?" + '\n')
outfile.write(str(number_characters) + '\n')
outfile.write("First sentence in the file?" + '\n')
outfile.write(str(first_sentence) + '\n')
outfile.write("Last sentence in the file?" + '\n')
outfile.write(str(last_sentence) + '\n')
outfile.write("Length of the first sentence" + '\n')
outfile.write(str(length_first)+ '\n')
outfile.write("Length of the last sentence" + '\n')
outfile.write(str(length_last) + '\n')
outfile.close()
def main(argv):
script = argv[0]
filename = argv[1]
input = open(filename, "r")
word_characters = GetCharactersWords(input)
number_characters = GetCharacterNumber(input)
first_sentence = FirstSentence(input)
length_first = LengthFirstSentence(input)
last_sentence = LastSentence(input)
length_last = LengthLastSentence(input)
file_print = print_to_file(word_characters, number_characters, first_sentence, length_first,
last_sentence, length_last)
if __name__ == '__main__':
main(argv)
Here is the output it gives me:
Here is the text file:
<built-in function input>
Number of words in the file?
2430
Number of sentences in the file?
0
First sentence in the file?
Last sentence in the file?
Length of the first sentence
0
Length of the last sentence
0
I can't figure out why it isn't giving me the different outputs.
Thanks for any help!