Presentation is loading. Please wait.

Presentation is loading. Please wait.

Main task -write me a program

Similar presentations


Presentation on theme: "Main task -write me a program"— Presentation transcript:

1 Main task -write me a program
Programming assessment Your task is to Create an algorithm-on paper pseudo code a program –on paper Write a program on python You must screenshot your code and explain You must write out your pseudocode Your code must be at least 15 lines long No copying of prior code allowed No internet allowed You must be quiet

2 Using Python 3 Demonstrating various coding techniques and ways in which to improve them

3 Lesson Objectives Lesson Outcomes
To look at how to create a random number within a given range To look at using IF, Elif, Else and functions To look at loops, lists and dictionaries Lesson Outcomes You will be able to write a program using random numbers with a parameter You will be able to index a list, create functions and set up a dictionary

4 Coding Learning to be an efficient programmer takes time. This PowerPoint will demonstrate ways of coding. Some of the early code is less efficient – purposefully so. You need to learn how to code before you start to realise how code can be made more reusable and efficient Functions allow us to break our problems down and are reusable Dictionaries can replace cumbersome IF, Elif statements Try to exercises and see how you can code and then how it is possible to improve your coding Remember to comment your code

5 Using random Python has a built in function called random. Random can be used in many different ways Typing in the period will show some of them

6 Using random for guessing game
We are going to create a game where the computer will create a random number between 1 and 10 and the user gets three attempts to guess it correctly

7 Using random for guessing game
The Algorithm in pseudo code Generate a random number While less than 3 attempts Input guess If guess > random Print (Too big) Elif guess < random Print (Too small) Else Print (well done)

8 Task 1 -Using random for guessing game
Setting up a random number

9 Using random for guessing game
Set up a loop so it can run three times

10 Using random for guessing game
Key points The random function will generate a random number between two givens The usertry = 1 before the loop starts The loop will run three times as it is incremented by 1 each time it runs If the user guesses the number the loop will stop as well

11 Full code with comments

12 Python – lists, IF statements and Functions
This program will demonstrate the use of Python lists, the IF statement and functions

13 Coding with Python The game Rock, Paper, Scissors has different winning combinations: Rock – beats scissors but loses to paper Paper – beats rock but loses to scissors Scissors – beats paper but loses to rock

14 Coding with Python - lists
Python lists are very versatile data types as they can hold a different data such as text, numbers etc. An example List = [‘Monday’, ‘Tuesday’, 11, ‘October’, 2014] [] brackets And commas to separate

15 Task 2 – IF, elif, else Our program will use a list to hold the values rock, paper, scissors Type the following – the def notvalid() will be explained later

16 Coding with Python The elements in the list have an index number so they can be referenced Lists start at zero, so the index for rock = 0, paper = 1 and scissors = 2 If you were to write print(computer[1]) it should print paper

17 Coding with Python Now we have a list we want the computer to select either rock, paper or scissors so we can try and beat it We will not know what the computer selects This time we can use another random method, one called choice

18 Coding with Python This basically means a random element will be chosen if the list, or other sequence, is not empty So we can use this method to select a random sequence from our list Note the list name is in the brackets

19 Coding with Python We place this code within our loop which we will use to run the game. Not the user input is converted to lower case like our list

20 Coding with Python Once inside the loop we need to cater for different scenarios The user can select rock The user can select paper The user can select scissors The user can mistype and not select any of them which would crash our program if we didn’t cater for it

21 Coding with Python – using IF
The IF statement is very powerful and can be used to return different messages and behaviour depending on the input The syntax: if something = this : do this elif something = this: elif something = this: else:

22

23 Coding with Python -Functions
The else statement at the bottom of our program called the function we wrote at the beginning

24 Coding with Python - Functions
Functions are much more efficient ways of coding programs as they can be reused and often they stop us producing duplicate code The program we just wrote could be rewritten using functions

25 Coding with Python Function syntax
Functions go at the top of the program They have an name that describes their purpose They use the def keyword and have () so values can be passed to them (parameters are optional) def printerror(parameter): body of function return value

26 Task 3 – Using Functions our function is defined with no parameters ()
it returns a message to the main body of the program

27 Coding with Python This time we will code the game again using functions we will have the following functions 1 for errors 1 for the user choosing rock 1 for the user choosing paper 1 for the user choosing scissors we will demonstrate passing arguments to a function and calling a function with no parameters We could put all the above functions into 1 function once we have mastered the use of functions

28 Coding with Python – Main()
Function calls for rock, paper, scissors and notvalid(). Note that rock, paper and scissors send an argument to the function; they pass the computer random selection

29 Functions – notvalid(), rock(), paper()

30 Functions - scissors()

31 Whole code

32 Whole code

33 Whole code

34 Using Functions, IF and Random()
Below is the output. The message are in brackets because they are read as a list element. We could get rid of these if necessary

35 Task 4 - Using 1 Function This program could be even more efficient if we were to reduce the amount of functions we used We could have one function for Rock, Paper and Scissors Task 4 - Try writing a function called: def workoutwinner(you, computer):

36 Using Functions When you program from now on try to break the problem down using functions as they are more efficient and can be reused Add comments to your code so you understand it later on # add a function

37 Task 5 - Using Dictionaries
The IF, Elif, Else code we have used in our program is not very efficient either. We could replace this code with a dictionary

38 The new code – Part 1

39 The new code – Part 2


Download ppt "Main task -write me a program"

Similar presentations


Ads by Google