Event Problem: File handling is an important technique that you need to accustom to it. File reading and writing are types of handling. Let’s practice file reading for now.
Create Event class with following attributes.
Attribute | DataType |
name | str |
hall_name | str |
contact | str |
cost | int |
leader | str |
Use __init__() method for initializing the above class attributes.
Create following method inside Event class.
Method | Description |
display() | This method is used to print the details of the event. |
Event Problem using Python
Input Filename : event.txt
Note
Details is stored in as comma separated values(name,hall_name,contact,leader,cost) in event.txt file.
Use read() method to read the data stored in event.txt file.
Sample output:
Event:Birthday party
Hall booked:Krishna Resort
Lead:Shashi
Cost:25000.00
Event:Marraige
Hall booked:Ashoka Hotel
Lead:Prasanna
Cost:75000.00
Event:Promotion party
Hall booked:6789012345
Lead:prasad
Cost:60000.00
Event:Engagement
Hall booked:Aliya mahal
Lead:Siddu
Cost:55000.00
Solution:
File: event.txt
class Event:
def __init__(self, name, hall_name, contact, leader, cost):
self.name = name
self.hall_name = hall_name
self.contact = contact
self.leader = leader
self.cost = cost
def display(self):
# fill your code here
print("Event:", self.name)
print("Hall booked:", self.hall_name)
print("Lead:", self.leader)
print("Cost:", self.cost)
from Event import Event
with open("event.txt", "r") as f:
for line in f:
line = line.strip()
line = line.split(",")
event = Event(line[0], line[1], line[2], line[3], line[4])
event.display()
print()