Concepts: Variables, Functions, Type Hinting, Logging.
You are building a small weather station script. Your task is to write a function that converts Celsius to Fahrenheit, but it must be “production-ready”.
Instructions:
logging module and configure it to level INFO.convert_c_to_f.float and return a float.(celsius * 9/5) + 32).info message: "Converting {celsius}°C to {fahrenheit}°F".Concepts: Lists, Loops, Conditionals, Debugging.
You have received a list of user ages from a database, but the data is dirty. Some values are strings, some are numbers, and some are negative (impossible!).
The Buggy Code:
Copy this into exercise_2.py. It currently crashes.
ages = [25, 30, "40", "not_available", 20, -5]
def calculate_average_age(age_list):
total = 0
count = 0
for age in age_list:
total += age
count += 1
return total / count
print(calculate_average_age(ages))
Instructions:
"40"), convert it to an int."not_available"), skip it.Concepts: Floating Point Math, Debugging, Logic.
You are processing payments for a transaction system. You have a wallet with 0.1 bitcoin, and you receive 0.2 bitcoin. You want to check if you now have exactly 0.3 bitcoin to execute a trade.
The Buggy Code: This code prints “Transaction failed?” even though 0.1 + 0.2 should equal 0.3.
wallet = 0.1
wallet += 0.2
print(f"Wallet balance:{wallet}")
if wallet == 0.3:
print("Transaction Success!")
else:
print("Transaction failed?")
Instructions:
Wallet balance: 0.3, yet the if check fails. This is confusing!if wallet == 0.3: line.wallet variable (or look in the Variables pane).wallet?round() function to fix the comparison (e.g., round to 1 decimal place).Concepts: File I/O, Strings, Context Managers (with).
A big part of Data Engineering is reading data from files. Let’s practice reading a raw text file and processing it.
Instructions:
raw_data.txt in your folder. Add these lines:amsterdam
rotterdam
the hague
utrecht
assignment_6.py.with open(...) pattern to read the raw_data.txt file.with open(...) again to write the cleaned names into a new file called processed_data.txt.Concepts: Dictionaries, Type Hinting, Logging, Complex Logic.
Combine everything! You need to process student grades.
Instructions:
student = {"name": "Alice", "grades": [85, 90, 78]}
process_student(student_data: dict) -> None.logging to show timestamps.INFO message: “Processing grades for [Name]”.INFO: “Grade A”.INFO: “Grade B”.WARNING: “Student requires assistance”.
Found a mistake or have a suggestion? Let us know in the feedback form.