Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Programming pt1

The document contains a programming assignment for students at Wallington High School for Girls, focusing on algorithms and Python programming. It includes pseudo-code examples, questions about the use of operators, and tasks to implement specific functionalities in Python, such as calculating charges for a theme park and simulating a dice game. The assignment is structured with multiple sections, each requiring students to demonstrate their understanding of programming concepts and syntax.

Uploaded by

Angi S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming pt1

The document contains a programming assignment for students at Wallington High School for Girls, focusing on algorithms and Python programming. It includes pseudo-code examples, questions about the use of operators, and tasks to implement specific functionalities in Python, such as calculating charges for a theme park and simulating a dice game. The assignment is structured with multiple sections, each requiring students to demonstrate their understanding of programming concepts and syntax.

Uploaded by

Angi S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Name: ________________________

Programming pt1 Class: ________________________

Date: ________________________

Time: 42 minutes

Marks: 31 marks

Comments:

Wallington High School for Girls Page 1 of 11


An algorithm, that uses the modulus operator, has been represented using pseudo-code in
1. Figure 1.

• Line numbers are included but are not part of the algorithm.

Figure 1

1 i ← USERINPUT
2 IF i MOD 2 = 0 THEN
3 OUTPUT i * i
4 ELSE
5 OUTPUT i
6 ENDIF

The modulus operator is used to calculate the remainder after dividing one integer by another.

For example:
• 14 MOD 3 evaluates to 2
• 24 MOD 5 evaluates to 4

(a) Shade one lozenge that shows the line number where selection is first used in the
algorithm in Figure 1.

A Line number 1

B Line number 2

C Line number 3

D Line number 4

(1)

(b) Shade one lozenge that shows the output from the algorithm in Figure 1 when the user
input is 4

A 0

B 2

C 4

D 8

E 16

(1)

Wallington High School for Girls Page 2 of 11


(c) Shade one lozenge that shows the line number where assignment is first used in the
algorithm in Figure 1.

A Line number 1

B Line number 2

C Line number 3

D Line number 4

(1)

(d) Shade one lozenge that shows the line number that contains a relational operator in the
algorithm in Figure 1.

A Line number 1

B Line number 2

C Line number 3

D Line number 4

(1)

Figure 1 has been included again below.

Figure 1

1 i ← USERINPUT
2 IF i MOD 2 = 0 THEN
3 OUTPUT i * i
4 ELSE
5 OUTPUT i
6 ENDIF

(e) Shade one lozenge to show which of the following is a true statement about the algorithm
in Figure 1.

A This algorithm uses a Boolean operator.

B This algorithm uses a named constant.

C This algorithm uses iteration.

D This algorithm uses the multiplication operator.

(1)

Wallington High School for Girls Page 3 of 11


(f) Figure 2 shows an implementation of the algorithm in Figure 1 using the Python
programming language.

• Line numbers are included but are not part of the program.

Figure 2

1 i = int(input("Enter a number: "))


2 if i % 2 == 0:
3 print(i * i)
4 else:
5 print(i)

The program in Figure 2 needs to be changed so that it repeats five times using definite
(count controlled) iteration.

Shade one lozenge next to the program that does this correctly.

A for x in range(0, 5):


i = int(input("Enter a number: "))
if i % 2 == 0:
print(i * i)
else:
print(i)
B for x in range(0, 6):
i = int(input("Enter a number: "))
if i % 2 == 0:
print(i * i)
else:
print(i)
C x = 1
while x != 6:
i = int(input("Enter a number: "))
if i % 2 == 0:
print(i * i)
else:
print(i)
x = x + 1
D x = 6
while x != 0:
i = int(input("Enter a number: "))
if i % 2 == 0:
print(i * i)
else:
print(i)
x = x – 1
(1)
(Total 6 marks)

Wallington High School for Girls Page 4 of 11


A theme park charges £15 per person for a daily ticket. If there are six or more people in a group,
2. the group is given a £5 discount.

Write a Python program to calculate the total charge for a group of people visiting the theme
park.

The program must:


• get the user to enter the number of people in a group
• calculate the total charge by:
○ charging £15 per person
○ reducing the total charge by £5 if there are six or more people
• output the total charge.

You should use indentation as appropriate, meaningful variable name(s) and Python syntax in
your answer.

The answer grid below contains vertical lines to help you indent your code.
(Total 6 marks)

Wallington High School for Girls Page 5 of 11


Wallington High School for Girls Page 6 of 11
(a) The figure below shows a line of Python code that creates a list of fruit names.
3.
fruits = ["banana", "apple", "orange", "pear",
"grape", "pineapple"]

Extend the program in the figure above. Your answer must be written in Python.

The program should get the user to enter a word and perform a linear search on the list
fruits to find if the word is in the list or not.

The program should:


• ask the user what word they would like to find
• output the message True if the word is found
• output the message False if the word is not found.

You must write your own linear search routine and not use any built-in search function
available in Python.

You should use indentation as appropriate, meaningful variable name(s) and Python
syntax in your answer.

The answer grid below contains vertical lines to help you indent your code.
(7)

Wallington High School for Girls Page 7 of 11


fruits = ["banana", "apple", "orange", "pear",
"grape", "pineapple"]

Wallington High School for Girls Page 8 of 11


(b) State why a binary search cannot be used on the list fruits

___________________________________________________________________

___________________________________________________________________
(1)
(Total 8 marks)

This question is about a dice game played against a computer.


4.
The aim of the game is to get as close to a score of 21 as you can, without going over 21. If your
score goes over 21 then you lose.

The player’s score starts at 0.

For each turn:

• two dice (each numbered from 1 to 6) are rolled


• the total of the two dice rolls is added to the player’s score
• the value of each dice and the player’s new total is output
• if the current score is less than 21, the player is asked if they would like to roll the dice
again: if the player says yes, they get another turn; otherwise, the game ends.

At the end of the game, the program should work as follows:

• if the final score is 21, output a message to say the player has won
• if the final score is greater than 21, output a message to say the player has lost
• if the final score is less than 21, the program generates a random number between 15 and
21 inclusive:
○ if this random number is greater than the player’s final score, output a message to
say the player has lost
○ otherwise, output a message to say the player has won.

The figure below shows the output of a program that plays this dice game.

Roll 1: 1
Roll 2: 4
Current score: 5
Would you like to roll again? yes

Roll 1: 1
Roll 2: 6
Current score: 12
Would you like to roll again? yes

Roll 1: 1
Roll 2: 2
Current score: 15
Would you like to roll again? yes

Roll 1: 6
Roll 2: 1
Current score: 22
You lost!

Wallington High School for Girls Page 9 of 11


Write a Python program to simulate this game.

The first line has been written for you in the answer grid.

The dice rolls are carried out by the program generating random numbers between 1 and 6. You
will need to use the Python function random.randrange(a, b) which generates a random
integer in the range a to b starting at a but finishing one before b.

You should use indentation as appropriate, meaningful variable name(s) and Python syntax in
your answer.

The answer grid below contains vertical lines to help you indent your code.
(Total 11 marks)

Wallington High School for Girls Page 10 of 11


import random

Wallington High School for Girls Page 11 of 11

You might also like