CODE CHALLENGE WORKED EXAMPLE:

Similar documents
Computer SCienCe Theme: Programming Techniques

COMPUTER SCIENCE. MCQs and Answers. MCQS Unit 2.3 Robust Programs Lesson 1 Testing Programs GCSE (9 1)

Unit 1.4 Wired and Wireless Networks

A LEVEL H446 COMPUTER SCIENCE. Code Challenges (21 40) September 2015

Computer SCienCe Theme: Applications Generation

AS and A LEVEL H046/H446 COMPUTER SCIENCE. Psuedocode Guide. August 2015

Topic Check In Plane isometric transformations

4. Work out the size of an interior angle of a regular 12-sided polygon.

OCR 10 Mensuration (Foundation)

Cambridge TECHNICALS LEVEL 3

Foundation Check In Straight line graphs

KS5-HE Transition Guide Checkpoint Task Instructions and answers for teachers

Cambridge TECHNICALS LEVEL 3

COMPUTER SCIENCE. H446/02 Summer 2017 examination series Set C Mid A LEVEL. Exemplar Candidate Work. H446 For first teaching in 2015

Properties of polygons

Cambridge TECHNICALS LEVEL 3

COMPUTER SCIENCE. H446/02 Summer 2017 examination series Set A High A LEVEL. Exemplar Candidate Work. H446 For first teaching in 2015

Lesson Element 11.02c Venn Diagrams. Instructions and answers for teachers

Cambridge TECHNICALS LEVEL 3

Foundation Check In Three-dimensional shapes

Cambridge TECHNICALS CAMBRIDGE TECHNICALS IN ENGINEERING LEVEL 3 UNIT 5 ELECTRICAL AND ELECTRONIC DESIGN. RESOURCES LINK April 2015

COMPUTER SCIENCE. H446/02 Summer 2017 examination series Set A Low A LEVEL. Exemplar Candidate Work. H446 For first teaching in 2015

Preparing for AS Level Further Mathematics

SAMPLE CURRICULUM GUIDE June 2014

Math Released Item Grade 7. Grid Coordinates of a Square VH225469

CSC101 - BMCC - Spring /22/2019. Lab 07

NEA sample solution Task 2 Cows and bulls (solution 2) GCSE Computer Science 8520 NEA (8520/CA/CB/CC/CD/CE)

Here is a sample IDLE window illustrating the use of these two functions:

GCSE Computer Science Booster Pack

Introduction to: Computers & Programming: Review prior to 2 nd Midterm

Degree Works Exceptions

Computer Science Foundation Exam. March 3, Section I A. No Calculators! Name: SSN: In this section of the exam, there are three (3) problems.

COMPUTER SCIENCE. H446/01 Summer 2017 examination series Set A Low A LEVEL. Exemplar Candidate Work. H446 For first teaching in 2015

An Introduction to Python for KS4!

Invent Your Own Computer Games with Python

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

2015 HSC Software Design and Development Marking Guidelines

RETURN X return X Returning a value from within a function: computes the value of variable exits the function and returns the value of the variable

Cambridge TECHNICALS LEVEL 3

Conditional Expressions and Decision Statements

Instructions for the 3 Spinner & More Programs

Basics of Programming with Python

COMPUTER SCIENCE. H446/01 Summer 2017 examination series Set A Mid A LEVEL. Exemplar Candidate Work. H446 For first teaching in 2015

Cambridge Assessment International Education Cambridge International Advanced Subsidiary and Advanced Level. Published

CS 115 Exam 3, Spring 2014

Math Released Item Geometry. Height of Support VF650053

OCR Pseudocode to Python

Math 101 Final Exam Study Notes:

How to Apply for a License in ELAR: A Step by Step Guide. October 2017

Chapter 6. Input Settings Screen

Friday 9 June 2017 Morning

Gradebook - Grades Tab Create Assignment

Grade Point Scales Standard Honors AP/College A B C D F Sample file

Lab 4: Strings/Loops Due Apr 22 at midnight

Solution 9: Data structures

Lab 4: Strings/While Loops Due Sun, Apr 17 at midnight

Getting started with. interactive whiteboards. Numicon software for

Thursday 19 June 2014 Morning

Contents Starting Up Excel... 1 Getting to Know Excel... 2 Workbook... 2 Sheet... 2 Cell... 2 Tabs, Tools, and Uses... 3 Home Tab...

COMPUTER SCIENCE. Project setting guidance A LEVEL. H446 For first teaching in Version 1

2001 Academic Challenge

RETURN X return X Returning a value from within a function: computes the value of variable exits the function and returns the value of the variable

HOW TO APPLY TO A TEACHER POSTING (For Continuing Teachers Only)

Chapter 3. Iteration

Integers and Rational Numbers

Chapter 5. Algorithms. Introduction. Chapter 5 Algorithms. Search algorithms. Linear search. Worked example

D. 90% 2. You need of a can of paint to paint one bench. How many cans would you need to paint 8 benches of equal size?

The Washington Professional Educator Standards Board. Sample Test Questions. Computer Science WA-SG-FLD073-01

TRIM-P.O.S. Software Corp. FLASHPOINT P.O.S. Software

Cambridge International Examinations Cambridge International Advanced Subsidiary and Advanced Level

Beyond programming. CS 199 Computer Science for Beginners Spring 2009 Lois Delcambre Week 5/12/2009 1

Ms. Payne, WCSS. This is very much like asking a question, and having a yes/no answer (also known a true/false). The general structure for this is:

Unit 3: Rational Numbers ANSWER KEY

Teaching guide: Structured programming

Recognition of Prior Learning - Guidance for Staff

LN #3 (3 Hrs) Repetition, Computational state CTPS Department of CSE,Coimbatore

Cambridge TECHNICALS LEVEL 3

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

MATHEMATICS SAMPLE ASSESSMENT MATERIALS

Advanced GCE Information and Communication Technology

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

SHORT ANSWER. Write the word or phrase that best completes each statement or answers the question.

TABLE OF CONTENTS 2 CHAPTER 1 3 CHAPTER 2 4 CHAPTER 3 5 CHAPTER 4. Algorithm Design & Problem Solving. Data Representation.

machine. The objective of the game is for the player to win as much money as possible and not go broke.

ECE15: Lab #4. Problem 1. University of California San Diego

SNS COLLEGE OF ENGINEERING

nformaweb Teacher Manual V Melb Mar

Cambridge Assessment International Education Cambridge International General Certificate of Secondary Education. Published

STUDENT REGISTRATION DATABASE (SRD) TABLE of CONTENTS

National 5 Computing Science Assignment Assessment task

Missouri State Educator Certification Q & A

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2016 Midterm I

Cambridge International Examinations Cambridge International Advanced Level

Programming Logic and Design Sixth Edition

Easy Chair Online Conference Submission, Tracking and Distribution Process: Getting Started

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Semester Project Report Mobile Application for Online Surakarta Battle

Unit title: Mobile Technology: Architecture (SCQF level 6)

TUTORIAL. HOW TO: - Edit with Microsoft Word - Send with Microsoft Outlook

9691 COMPUTING. 9691/21 Paper 2 (Written Paper), maximum raw mark 75

Transcription:

CODE CHALLENGE WORKED EXAMPLE: FRUIT MACHINE For each challenge, solve it using: A flowchart Pseudocode (see A Level Pseudocode Guide http://www.ocr.org.uk/images/202654-pseudocode-guide.pdf ) Program code (any high level language will do). The challenge: Fruit Machine Write a program to simulate a Fruit Machine that displays three symbols at random from Cherry, Bell, Lemon, Orange, Star, Skull. The player starts with 1 credit, with each go costing 20p. If the Fruit Machine rolls two of the same symbol, the user wins 50p. The player wins 1 for three of the same and 5 for three Bells. The player loses 1 if two skulls are rolled and all of his/her money if three skulls are rolled. The player can choose to quit with the winnings after each roll or keep playing until there is no money left. Useful Resources: choose https://docs.python.org/3.3/tutorial/datastructures.html

The flowchart: Start Set credit to 1.0 Is input!= n and credit >= 0.2 Yes Input y or n to play subtract 20p from the credit and then spin the slots randomly generate 3 symbols No Do 3 symbols match? No Do 2 symbols match? Yes Yes add/subtract from score depending on symbols End

Pseudocode: Pseudocode symbols = ( Cherry, Bell, Lemon, Orange, Star, Skull ) function main() credit = 1.0 usrinput = while usrinput!= n AND credit >= 0.2 round(credit,2) usrinput = input( Would you like to play again? (y/n) ) if usrinput == y then credit -= 0.2 credit += SpinSlots(credit) endif endwhile print( Goodbye. Thanks for playing ) endfunction Explanation 1. We first create a tuple of all possible symbols in the fruit machine 2. In the main function we set up the initial credit, and while the user still has enough credit to play and still wants to, we call the function SpinSlots, passing in the credit available, taking away 20p for each go. function SpinSlots(credit) winnings = 0.0 print( Spinning... ) #start with blank list to store spins outcome = [] for i =0 to 2 #There are 6 symbols in the list number = random.randint(0,5) outcome.append(symbols[number]) outcome.sort() print(outcome) #3 of the same if all(x == outcome[0] for x in outcome) then if outcome[0] == Bell then #3 bells win 5 pounds winnings = 5.0 3. At the start of the SpinSlots function we set winnings equal to 0 and then randomly generate 3 symbols using the tuple we set up earlier. 4. It is now a job of processing this list to see how much we win from this round.

else endif elseif outcome[0] == Skull then else endif #3 skulls lose all your money winnings = 0.0-credit #3 of any symbol win 1 pound winnings = 1.0 #2 of same symbol prev = None for item in outcome: if prev == item then if item == Skull prev = item #2 skulls lose a pound winnings = -1.0 #2 of any symbol win 50p winnings = 0.5 print( Earnings for this round: + str(round(winnings,2))) return winnings endfunction 5. We return the winnings (whether negative or positive) and add this to the existing credit.

Program code (with comments): symbols = ( Cherry, Bell, Lemon, Orange, Star, Skull ) def main(): credit = 1.0 usrinput = while usrinput!= n and credit >= 0.2: round(credit,2) usrinput = input( Would you like to play again? (y/n) ) if usrinput == y : credit -= 0.2 credit += SpinSlots(credit) print( Goodbye. Thanks for playing ) def SpinSlots(credit): winnings = 0.0 print( Spinning... ) #start with blank list to store spins outcome = [] for i in range(3): #There are 6 symbols in the list number = random.randint(0,5) outcome.append(symbols[number]) outcome.sort() print(outcome) #3 of the same if all(x == outcome[0] for x in outcome): if outcome[0] == Bell : #3 bells win 5 pounds winnings = 5.0 elif outcome[0] == Skull : #3 skulls lose all your money winnings = 0.0-credit #3 of any symbol win 1 pound winnings = 1.0

#2 of same symbol prev = None for item in outcome: if prev == item: if item == Skull : #2 skulls lose a pound winnings = -1.0 #2 of any symbol win 50p winnings = 0.5 prev = item print( Earnings for this round: + str(round(winnings,2))) return winnings We d like to know your view on the resources we produce. By clicking on the Like or Dislike button you can help us to ensure that our resources work for you. When the email template pops up please add additional comments if you wish and then just click Send. Thank you. If you do not currently offer this OCR qualification but would like to do so, please complete the Expression of Interest Form which can be found here: www.ocr.org.uk/expression-of-interest OCR Resources: the small print OCR s resources are provided to support the teaching of OCR specifications, but in no way constitute an endorsed teaching method that is required by the Board and the decision to use them lies with the individual teacher. Whilst every effort is made to ensure the accuracy of the content, OCR cannot be held responsible for any errors or omissions within these resources. We update our resources on a regular basis, so please check the OCR website to ensure you have the most up to date version. OCR 2015 - This resource may be freely copied and distributed, as long as the OCR logo and this message remain intact and OCR is acknowledged as the originator of this work. OCR acknowledges the use of the following content: Thumbs up: alex_white/shutterstock.com, Thumbs down: alex_white/shutterstock.com Please get in touch if you want to discuss the accessibility of resources we offer to support delivery of our qualifications: resources.feedback@ocr.org.uk