PCEP Computer Programming and Python Fundamentals • Set 3
PCEP Computer Programming and Python Fundamentals Practice Test 3 — 15 questions with explanations. Free, no signup.
A junior developer is tasked with writing a Python script that reads a list of integers from a file, removes any duplicate numbers, and then writes the unique numbers back to the same file in ascending order. The file 'numbers.txt' currently contains one integer per line. The developer writes the following code:
with open('numbers.txt', 'r') as f:numbers = [int(line.strip()) for line in f] unique = list(set(numbers)) unique.sort()
with open('numbers.txt', 'w') as f:
for num in unique:f.write(str(num) + '\n')
The script runs without errors, but the output file contains the numbers in descending order instead of ascending. The developer checks the sort() method and confirms it sorts in ascending order. What is the MOST likely cause of the issue?
Choose an answer to begin — your selection is scored in the full session.
15 questions · instant feedback and full explanations after every question.