Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN PYTHON 3.4.1: Suppose you have a file named heroes.cvs with the following con

ID: 3771605 • Letter: I

Question

IN PYTHON 3.4.1:

Suppose you have a file named heroes.cvs with the following content:

Iron Man, 4, citizen, 4
Super Man, 71, resident, 21
Flash Gordon, 56, citizen, 50
Wonder Woman, 44, citizen, 9
Hulk, 102, resident, 56

write a program that reads the file with all the heroes. Your program outputs to the console only the people who are older than 39, and have been a citizen for 9 or more years. Format the output as shown below:

example output:

Hero: Flash Gordon, Age: 56, Status: citizen, Duration:50

Hero:Wonder Woman, Age: 44, Status: citizen, Duration:9

Explanation / Answer

import csv
with open('D:/ravi/Cheg/heroes.csv', newline='') as f:
   reader = csv.reader(f)
   for row in reader:
      
       age = int(row[1])
       duration = int(row[3])
       if(age > 39 and row[2].strip() == "citizen" and duration >= 9):
           print("Hero: ", row[0] ," Age: ", age, " Status: ", row[2], " Duration: ",duration)

---output---

Hero: Flash Gordon   Age: 56   Status:   citizen Duration: 50
Hero: Wonder Woman   Age: 44   Status:   citizen Duration: 9