In this lab, you open a file and read input from that file in a prewritten Pytho
ID: 3777377 • Letter: I
Question
In this lab, you open a file and read input from that file in a prewritten Python program. The program should read and print the names of flowers and whether they are grown in shade or sun. The data is stored in the input file named flowers-dat. Open the source code file named Flowers.py Declare the variables you will need. Write the Python statements that will open the input file flowers dat for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and Where it can be grown (sun or shade). Execute the program. # Flowers .py - This program reads names of flowers and whether they are grown in shade or sun from an input # file and prints the Information to the user's screen. # Input: flowers.dat. # Output: Names of flowers and the words sun or shade. # Declare variables here # Open input file Write while loop that reads records from file, fin = open('flowers dat") Print flower name using the following format # print(var +^- grows in teh^- + var 2) fin. close ():Explanation / Answer
Answer
Below is the required python code:
f = open("flowers.dat","r") #opens file with name "flowers.dat" in read mode
flowerList = [] #take an empty list
for line in f: # for loop is better to keep track of the end of the file
flowerList.append(line) #each line gets appended to a list
print(flowerList) #list gets printed
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.