Question

Python Error: Writing a program to complete the following: Prompt the user for the name of...

Python Error: Writing a program to complete the following:

  • Prompt the user for the name of an input file.
  • Open that file for input. (reading)
  • Read the file using a "for line in a file" loop
  • For each line,  o print the line and compute and print the average word length for that line.
  • Close the input file Sample output:

MY CODE IS WRONG AND NEED HELP CORRECTING IT!!!

------------------------------------------------------------------------------------------------------------------------

DESIRED OUTPUT:

Enter input file name: Seasons.txt

Thirty days hath September

5.75

April, June, and November

5.5

All the rest have thirty-one

4.8

Except February, which has twenty-eight.

7.2

-------------------------------------------------------------------------------------------------------------

CODE I HAVE WRITTEN (WRONG):

def main():

    # Prompt the user for the name of an input file.

    file=input("Enter input file name: ")

    # Open file.

    infile=open(file, "r")

    aLine = infile.readline()

    phrase=aLine

    # Use split to break phrase into individual strings.

    words = phrase.split()

    # Define word count

    wordCount = len(words)

    # Calculate average word count.

    for word in words:

        sum = 0

        avg = 1

        ch = len(word)

        sum = sum + ch

        avg = sum / wordCount

    print("A: ",aLine,avg)

    aLine = infile.readline()

    print("B: ",aLine,avg)

    aLine = infile.readline()

    print("C: ",aLine,avg)

    aLine = infile.readline()

    print("D: ",aLine,print(avg))

    infile.close()

main()

------------------------------------------------------------------------------------------------------------------------

FILE IS CALLED: "Seasons.txt"

CONTENT:----------------------------------------------------------------------------------------------------------

Thirty days hath September
April, June, and November
All the rest have thirty-one
Except February, which has twenty-eight.

------------------------------------------------------------------------------------------------------------------------

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Seasons.txt

Thirty days hath September
April, June, and November
All the rest have thirty-one
Except February, which has twenty-eight.

#source code:

def main():
file=input("Enter input file name:")
f=open(file,"r")
for line in f.readlines():
sub=line.split(" ")
for i in range(len(sub)):
sub[i]=sub[i].replace("\n","")
count=0
word=0
for i in range(len(sub)):
count+=1
word+=len(sub[i])
print(sub[i],end=" ")
print("\n",round(word/count,2))
main()

#output:

#if you have any doubt comment below..if you like give thumbs up....

Add a comment
Know the answer?
Add Answer to:
Python Error: Writing a program to complete the following: Prompt the user for the name of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I cant get this python program to read all the unique words in a file. It...

    I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • Write a PYTHON program that reads a file (prompt user for the input file name) containing...

    Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1   0.5 2   0.5 3   0.5 4   0.5 The output should be:    The averages are 2.50 and 0.5. a)   Your code with comments b)   A screenshot of the execution Version 3.7.2

  • C++ please! (1) Prompt the user to enter the name of the input file. The file...

    C++ please! (1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string. Ex: Enter file name: input1.txt File content: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! (2) Implement a PrintMenu() function,...

  • Python Problem Hello i am trying to finish this code it does not save guesses or...

    Python Problem Hello i am trying to finish this code it does not save guesses or count wrong guesses. When printing hman it needs to only show _ and letters together. For example if the word is amazing and guess=a it must print a_a_ _ _ _ not a_ _ _ _ and _a_ _ _ separately or with spaces. The guess has a maximum of 8 wrong guesses so for every wrong guess the game must count it if...

  • I need python help .. I need to know if a user were to input 3...

    I need python help .. I need to know if a user were to input 3 commands to a program and run it such as specify: Usage: ./filemaker INPUTCOMMANDFILE OUTPUTFILE RECORDCOUNT (./filemaker is the program)( (INPUTCOOMANDFUILE= a text file that contains key words that need to be searched through then decided what to do ) (RECORDCOUNT= number) Given an input file that contains the following: That specifies the text, then the output, and the number of times to be repeated...

  • Complete the Python program below that performs the following operations. First prompt the user to input...

    Complete the Python program below that performs the following operations. First prompt the user to input two integers, n and m. If n<m, then print the odd positive integers that are less than m (in order, on a single line, separated by spaces). If man, then print the even positive integers that are less than n (in order, on a single line, separated by spaces). If neem, then print nothing. For instance, if the user enters 5 followed by 10,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT