Files and Functions Problem Sheet
Files and Functions Problem Sheet
1. A group of friends is planning a road trip. They want to share the cost of the fuel equally
among themselves.Write a function fuel_cost(distance_km, mileage_kmpl,
fuel_price_per_litre, num_people) that calculates the total fuel cost for the trip and then splits
it evenly among all travelers.
2. Imagine an airline wants to offer special discounts to make flying more affordable.
Children under 12 years old, senior citizens over 60 years old, and students are eligible for
discounted fares. Create a function named calculate_flight_fare(base_fare, age, is_student)
that calculates the final ticket price based on a passenger’s age and student status.
The function should adjust the original fare by applying the appropriate discount rules.
3. In a town where everyone loves staying connected, a mobile company offers a special data
plan: the first 2 GB of data usage every month is absolutely free! However, once a customer
crosses 2 GB, they must pay ₹50 for every additional GB used. Write a function named
data_bill(data_used_gb) that helps users calculate their total bill based on how much data
they have used in a month.
FILE
2. Harry is managing system configuration files for a company. Before making any updates to
the configuration file, he wants to back up the original file to ensure he can restore it if
necessary. Write a Python program that copies the content of config.txt to a backup file
named config_backup.txt.
3. Jenny is analyzing text documents to gather content statistics, such as the number of
characters, words, and lines in a document. This analysis is required for content quality
checks or text processing tasks. Write a Python program that opens a file named
document.txt, counts the number of characters, words, and lines in the file, and prints the
results.
4. ABC institution is maintaining employee records in a text file where each line contains the
employee’s ID, name, department, and salary. You need to update the salary of a specific
employee based on their ID. Write a Python program that opens a file called
employee_records.txt, searches for an employee by their ID, and updates their salary. Save
the updated employee data back to the file.
5. You are working on a platform that allows users to post comments. You need to ensure that
certain offensive words are censored in the comments. These words are stored in a list, and
you need to remove or replace them with asterisks (****). Write a Python program that
reads comments from a file named comments.txt, searches for offensive words from a
predefined list, and replaces them with asterisks. Save the modified comments back to the
file.
6. Vinu is working on a content management system where articles are stored in text files. He
needs to update specific terminology throughout the articles (e.g., replace the term "old_term"
with "new_term"). Write a Python program that opens a file called article.txt, searches for a
specific word (old_term), and replaces it with new_term. Save the modified content back to
the file.
7. You are working on a user feedback system. User feedback is stored in a file, and each
piece of feedback contains a timestamp, user ID, and comments. You need to append new
feedback and then sort the feedback based on the timestamp. Write a Python program that
appends new feedback to the feedback.txt file and then sorts all the feedback based on the
timestamp. Save the sorted feedback back to the file.