A simulation of a temperature control system is designed to adjust the temperature in steps of 3∘C3^\circ\text{C}3∘C starting from 0∘C0^\circ\text{C}0∘C until it reaches a user-specified target temperature.
The Python program below is intended to display the sequence of intermediate temperature steps.
temp = 0
target = int(input("Enter a target temperature (a multiple of 3): "))
while temp != target:
print(temp)
temp = temp + 3
print("Target reached!")
The program works correctly if the target temperature entered by the user is positive. For example, if 9 is entered, the program correctly displays the values 0, 3, and 6 before stopping.
However, the program does not work correctly if a negative target temperature is entered by the user. For example, when -9 is entered, the program should display the values 0, -3, and -6, but it enters an infinite loop instead.
Using Python only, change the program code inside the while loop so that it will work correctly for any target temperature (positive, negative, or zero) that is a multiple of 3.
298 exam-style questions on AQA GCSE Computer Science Programming concepts. Each one has a worked solution and a mark scheme showing where the marks go.