A program calculates a worker's pay. The number of hours worked is always a whole number and the hourly rate is in pounds, for example 11.44.
01hours = input("Enter hours worked")
02rate = input("Enter hourly rate")
03pay = hours * rate
04print("Your pay is " + pay)
The program does not work because of the data types used. Rewrite each line that needs to change, using OCR Exam Reference Language or a high-level programming language, and explain why the changes are needed.
Model answer
hours = int(input("Enter hours worked"))
rate = real(input("Enter hourly rate"))
pay = hours * rate
print("Your pay is " + str(pay))
Lines 01 and 02 change because input returns strings, which cannot be multiplied together. Line 04 changes because pay is a number, which must be cast to a string before it is joined to the text.
Explanation
Hours is a whole number, so it is cast to an integer; the rate has pence, so it is cast to a real. Only then can line 03 multiply them. Line 04 joins text to the number with +, which needs str(). Line 03 itself is fine once the inputs are numbers - a common mistake is to rewrite the calculation but leave the output line unchanged.