Question

Write a program that reads the name of fruits and the colors from an input file fruit. txt. Each fruit name and the color is
python
0 0
Add a comment Improve this question Transcribed image text
Answer #1
filename = 'fruit.txt'

try:
    f = open(filename, 'r')
    for line in f:
        words = line.strip().split()
        print("Color of {} is {}".format(words[0], words[1]))
    f.close()
except FileNotFoundError:
    print(filename + " does not exists!")
Add a comment
Know the answer?
Add Answer to:
python Write a program that reads the name of fruits and the colors from an input...
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
  • Python problem: 1. The colors red, blue, and yellow are known as the primary colors because...

    Python problem: 1. The colors red, blue, and yellow are known as the primary colors because they cannot be made by mixing other colors. When you mix two primary colors, you get a secondary color, as shown here: When you mix red and blue, you get purple. When you mix red and yellow, you get orange. When you mix blue and yellow, you get green. Create a text file with the names of two colors in it. Write a program...

  • 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

  • Write a Python program that reads user input from the command-line. The program should define a...

    Write a Python program that reads user input from the command-line. The program should define a function read Position, which reads the values for t, v0, and h0. If there is an IndexError, print 'Please provide the values for t, vO, and hO on the command line.'. If t, v0, or h0 are less than 0. print 't = # is not possible.' for each variable respectively. Note that the # represents the number entered on the command-line by the...

  • using Python please reverse file Writ te a function named reverse file. This function will take...

    using Python please reverse file Writ te a function named reverse file. This function will take one parameter which will be the file name of the file to reverse. This function will open the file with the name passed in as a parameter and will print out all the lines of the file in reverse order. Below are a few examples: If the files lines.brt looks like: orange apple orange apple orange apple grape Then the following should look like...

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • 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...

  • 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”...

  • I/O program for C Write a program in direct1.c which reads from standard input a line...

    I/O program for C Write a program in direct1.c which reads from standard input a line and then outputs that line immediately to standard output. it should read the first line only ! Then, write another program called direct2.c which reads from standard input every line until end of input and outputs them to standard output. Everything that goes in should come out exactly as it was. Compile both programs and run them on the input files given below The...

  • Task 4: for Loops (PROGRAM IS IN PYTHON) SHOW INPUT AND OUTPUT SCREEN SHOTS #!/usr/bin/python3 # ...

    Task 4: for Loops (PROGRAM IS IN PYTHON) SHOW INPUT AND OUTPUT SCREEN SHOTS #!/usr/bin/python3 # Measure some strings: words = ['cat', 'window', 'defenestrate'] for w in words: print(w, len(w)) #!/usr/bin/python3 # Measure some strings: words = ['cat', 'window', 'defenestrate'] for w in words: print(w, len(w)) forvar in list(range(5)): print (var) for letter in 'Python': # traversal of a string sequence print ('Current Letter :', letter) print() fruits = ['banana', 'apple', 'mango'] for fruit in fruits: # traversal of List...

  • 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...

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