OCR J277: Python Coding Challenges
Practice writing Python code for the following scenarios. Pay attention to the marks available for each question.
Question 1: Basic Input and Arithmetic [3 marks]
A cinema charges £10 for adult tickets and £5 for child tickets. Write a Python program that:
- Asks the user to input the number of adult tickets required.
- Asks the user to input the number of child tickets required.
- Calculates and prints the total cost.
Question 2: Selection [4 marks]
A teacher wants a program to grade a recent test out of 50. Write a Python program that:
- Takes the student's test score as an input.
- Outputs "Distinction" if the score is 40 or above.
- Outputs "Merit" if the score is between 30 and 39 (inclusive).
- Outputs "Pass" if the score is between 20 and 29 (inclusive).
- Outputs "Fail" if the score is below 20.
Question 3: Iteration and Strings [4 marks]
Write a Python program that asks the user to input a single word. The program should loop through the word and count how many times the letter "a" (lowercase) appears. It should then output the final count.
Question 4: Arrays (Lists) and Aggregation [5 marks]
An array contains the following daily temperatures: temps = [12, 15, 14, 18, 11].
Write a Python program that iterates through this array to:
- Calculate the total sum of the temperatures.
- Find the highest temperature in the array.
- Output both the total sum and the highest temperature.
Question 5: Robust Validation Loop [6 marks]
A system requires a user to set a secure PIN. Write a Python program that:
- Continually asks the user to enter a new PIN.
- Checks that the PIN is exactly 4 characters long.
- If the PIN is not 4 characters long, outputs an error message and loops back to ask again.
- If the PIN is valid, outputs "PIN accepted" and stops the loop.