1.Write a file, then read it
IntermediateWrite three lines (Asha,88, Ravi,95, Meera,79) to a file called scores.txt. Then read it back and print each name with its score, and the highest score.
Show hint
Open with "w" to write and "r" to read, inside with blocks. Each line needs a \n when writing, and .strip() when reading.
Show solution
with open("scores.txt", "w", encoding="utf-8") as f:
f.write("Asha,88\n")
f.write("Ravi,95\n")
f.write("Meera,79\n")
best = 0
with open("scores.txt", "r", encoding="utf-8") as f:
for line in f:
name, score = line.strip().split(",")
score = int(score)
print(name, score)
best = max(best, score)
print("Highest:", best)Asha 88
Ravi 95
Meera 79
Highest: 95The with statement closes the file automatically, even if an error happens inside the block.