Main task -write me a program

Slides:



Advertisements
Similar presentations
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Advertisements

Structured programming
HW 1: Problems 3 & 4 EC 1 & 2.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
CS 201 Functions Debzani Deb.
Fundamentals of Python: From First Programs Through Data Structures
The University of Texas – Pan American
Fundamentals of Python: First Programs
An Introduction to Textual Programming
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Chapter 5: Control Structures II (Repetition)
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Control Structures FOR Statement Looping.
General Programming Introduction to Computing Science and Programming I.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
By the end of this session you should be able to...
If statements while loop for loop
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
1. Understand the application of Pseudo Code for programming purposes 2. Be able to write algorithms in Pseudo Code.
The Software Development Process
If the same piece of code needs to be used several times we can use a loop – but only if those times are all together. If you need to run the same bit.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Use Flowchart modeling to design and create an interactive quiz to be run on Powerpoint. Start Question Click to start the quiz Correct Incorrect, try.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
Progression in KS3/4 Algorithms MONDAY 30 TH NOVEMBER SUE SENTANCE.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
MOOR ERROR HANDLING Types of error How to test your code for errors How to detect errors How to recover from errors.
This is a while loop. The code is done while the condition is true. The code that is done is enclosed in { }. Note that this program has three parts: Housekeeping.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
3.1 Fundamentals of algorithms
Introduction to Programming
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
7 - Programming 7P, Q, R - Testing.
Functions.
Writing Functions( ) (Part 5)
Passing Parameters by value
Do While (condition is true) … Loop
Computational Thinking for KS3
Introduction to Programming
Programming We have seen various examples of programming languages
Writing Functions( ) (Part 4)
PYTHON: BUILDING BLOCKS Sequencing & Selection
CISC101 Reminders All assignments are now posted.
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Introduction to Programming
Presentation transcript:

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

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

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

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

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

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

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)

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

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

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

Full code with comments

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

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

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

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

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

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

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

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

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

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:

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

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

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

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

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

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

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

Functions - scissors()

Whole code

Whole code

Whole code

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

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):

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

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

The new code – Part 1

The new code – Part 2