Select a Lesson: »Lesson 1 Basic syntaxLesson 1 Basic syntax »Lesson 2 Escape sequencesLesson 2 Escape sequences »Lesson 3 Mathematical functionsLesson.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Fundamentals of Python: From First Programs Through Data Structures
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
An Introduction to Textual Programming
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
22/11/ Selection If selection construct.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
NAME Python Programming Workbook Select a Lesson:
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
NAME Python Programming Workbook Select a Lesson:
GCSE Computing: Programming GCSE Programming Remembering Python.
Getting Started With Python Brendan Routledge
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
CompSci 230 S Programming Techniques
Fundamentals of Programming I Overview of Programming
GCSE COMPUTER SCIENCE Practical Programming using Python
Whatcha doin'? Aims: To start using Python. To understand loops.
Topics Designing a Program Input, Processing, and Output
Chapter 2 Introduction to C++ Programming
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
IF statements.
Introduction to Programming
Presented By S.Yamuna AP/IT
The Selection Structure
Variables, Expressions, and IO
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Engineering Innovation Center
Lesson 1 Learning Objectives
Selection CIS 40 – Introduction to Programming in Python
Fill the screen challenge!
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Learning Outcomes –Lesson 4
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
If selection construct
Introduction to Programming
If selection construct
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2: Introduction to C++.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Flowcharts and Pseudo Code
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Programming In Lesson 4.
Beginning Python Programming
12th Computer Science – Unit 5
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Hardware is… Software is…
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Select a Lesson: »Lesson 1 Basic syntaxLesson 1 Basic syntax »Lesson 2 Escape sequencesLesson 2 Escape sequences »Lesson 3 Mathematical functionsLesson 3 Mathematical functions »Lesson 4 ErrorsLesson 4 Errors »Lesson 5 VariablesLesson 5 Variables »Lesson 6 StringsLesson 6 Strings »Lesson 7 Relational operatorsLesson 7 Relational operators »Lesson 8 using ELIFLesson 8 using ELIF

Lesson 1

Programming screen Activity 1.1- Write a program to print out “Hello World” Python Shell Did you have to correct any errors? If so, what were they? Common mistakes include missing the parenthesis and capitalising the P in print.

Programming screen Activity 1.2- Write a program that writes your name on the screen Python Shell Did you have to correct any errors? If so, what were they? Common mistakes include missing the parenthesis and inverted commas. The +name must also be outside of the inverted commas.

Activity 1.3 practice- Copy the following code into python

Programming screen Activity 1.3- Write a program that prints your name six times Python Shell Did you have to correct any errors? If so, what were they?

Lesson 2

Activity 2.1- True or false Python was released in 2010false Python was named after the TV series “Monty Python’s Flying Circus” True Python is proprietary software which is expensive to buy false Python was written by Bill Gatesfalse YouTube is written in PythonTrue Are the below statement True or False? Highlight the correct answers!

Activity 2.2- Keyboard shortcuts Make a note of what these useful keyboard shortcuts do! Keyboard shortcut What does the keyboard shortcut do? Control c Copy selected text Control v Paste Control a Select all Control x Cut the selected text Control f Find Control n Opens a new window Control p Prints Control s Saves Control z Undo

Activity 2.3 practice- Copy the following code into python

Programming screen Activity 2.3- Write a program that writes four lines of the lyrics of your favourite song on the screen Python Shell Did you have to correct any errors? If so, what were they?

Activity 2.4 – Copy and run this program Python Shell 1.What happens? The program prints the words the rain falls from the sky. 2.What effect does the ”,“ have? There is one white space in the place of the comma.

Escape sequences can be used to alter how the information is displayed on the screen. An escape sequence is a back slash “\”. Experiment with these escape sequences and complete the table. Hint: Use alt p to display the last command entered in the IDLE shell Activity 2.5 – Escape sequences Escape sequenceEffect \t Tab \n New line \\ Displays \ \’ Displays ‘ \” Displays “

Programming screen Activity 2.6- Print this text using one line of code Did you have to correct any errors? If so, what were they? Common errors include placing a space after the \n and using the wrong forward slash e.g. /.

Activity 2.7- Write a program using print commands to display your initials five characters high on the screen. For example:

Programming screen Activity 2.7- Write a program using print commands to display your initials five characters high on the screen. Did you have to correct any errors? If so, what were they? Python Shell

A Activity 2.8- An algorithm is a sequence of steps needed to perform a particular task. Algorithms can be represented in different ways. Match the words to the representations on the next three slides. Program code

B RECEIVE myName FROM (STRING) KEYBOARD RECEIVE myAge FROM (INTEGER) KEYBOARD SET AgeInTen TOmyAge + 10 SEND myName “will be” AgeInTen “in 10 years time” TO DISPLAY C Write a program that prompts the user to enter their name and age. The program then adds 10 onto the age and displays the text “ will be in ten years time.” Match the words to the representations. Pseudocode Written description

E D Match the words to the representations. If animal has 4 legs then If animal has a tail then If animal answers to “puss” then animal = cat endif Flowchart Structured English

Activity 2.9- Explore ASCII art (Wikipedia) and write a Python program to display your ASCII art creation. For example:

Programming screen Activity 2.9- Explore ASCII art (Wikipedia) and write a Python program to display your ASCII art creation. Python Shell Did you have to correct any errors? If so, what were they?

Lesson 3

Activity 3.1- Copy and run these lines of code. Complete the table to explain what the mathematical operators do. >>> 60/5 >>> >>> 564*89 >>> 2**5 >>> 43-5 >>> 11//2 >>> 11%2 Mathematical operator symbol Operation /Divide +add *multiply **exponential -subtract //Integer division %Modulus (Remainder after the division)

Activity 3.2- Make up some mathematical calculations of your own and add an example to the table for each mathematical operator. Mathematical operator symbol ExampleAnswer / 10/ *10*220 **10** //10//25 %10%20

Programming screen Activity Write a program to display this text on the screen and fill in the missing number. 8 cats have 4 legs each The cats have ___ legs in total Python Shell

Programming screen Activity Write a program to display this text on the screen and fill in the missing number. A farmer with 1089 sheep sells 56 of them The farmer has _____ sheep left Python Shell

Programming screen Activity Write a program to display this text on the screen and fill in the missing number. 4 children pick 56 flowers each The children each have ____ flowers Python Shell

Programming screen Activity 3.4- Copy and run these lines of code. What effect do the parentheses () have? The parentheses control the order in which the numbers are calculated. Anything in parentheses is evaluated first. Python Shell

Activity 3.5- Predict what you think will be displayed on the screen when this line of code is executed. Prediction: 24.5 Now copy and run the code. Explain the answer you get. Is it what you predicted? Multiplication and division have higher precedence and take place first from left to right. First the division: 7.5 * Then the multiplication giving: Then the addition giving: 24.5

Activity 3.6- Make up some multiple-choice questions on precedence. Each question must have four possible answers, one of which is correct. Check your answers are correct by using the interactive Python shell. Try your questions out on other people and document this using the template on the next slide. You must be able to explain to them why the answers are correct.

Question Activity 3.6- Make up some multiple-choice questions on precedence. Can you explain how you got the outcome you did? Outcome

Activity 3.7- Devise your own mnemonic for remembering the order of precedence. Python order of precedenceMnemonic Parenthesis (brackets) Exponential Division and multiplication Addition and subtraction

Lesson 4

Activity Copy the following code into python Explain the results SyntaxError: invalid syntax In computer science ‘syntax’ means the set of rules that describes how the language is constructed. A syntax error means that Python does not know what to do because the language used does not follow the correct rules. It does not print hello world as it is not within parenthesis or inverted commas. Programming screenPython Shell

Activity Copy the following code into python Explain the results – It prints hello world as it is within parenthesis or inverted commas. Programming screen|Python Shell

Activity 4.3- Python produces different types of error. Match up the type of error with its description. Description of the error Dividing a number by zero. When a program is interrupted from the keyboard by pressing control+c. When a name is used that is not known about (often a variable name that is misspelt). When an operation is attempt that is invalid for that type of data. An error occurs when the program is running. Type of error TypeError RuntimeError NameError ZeroDivisionError KeyBoardInterrupt Hint: A full list of Python errors can be found here: utorialspoin t.com/python /standard_ex ceptions.htm utorialspoin t.com/python /standard_ex ceptions.htm

Activity Copy the following code into python Explain the results Nothing happens as the # shows that this is a comment statement which the Python program ignores. Always use comments to make your programs readable. Include your name and date and an explanation of what the program does at the beginning of every program. Programming screenPython Shell

Activity Copy the following code into python Use Copy this code into the file. Save the file using file/save. Always give the filename an extension of.py. Use a meaningful filename and store in a suitable folder. Run the commands by using run/run module Close Python and then restart Python, open the file and run the program again.

Activity Copy the following code into python Following the same steps as in Activity 4.5, write a program that displays your name and then your age. Hint: Use file/new window and save the file using file/save. Don’t forget to use a meaningful filename Programming screen Python Shell

Activity 4.7 – Using the python IDLE Write your own summary sheet to describe how to run Python programs, including how to save and open.py files. Include any other commands that you have found useful. Using the Python IDLE It helps to avoid feature clutter How to open a new window Open up a fresh shell to begin typing up new code How to save a file Ctrl + S How to open a file Open a file that has been saved but it will only work if it is saved as a.py file. How to run a program f5 Useful tips alt pDisplays the last line you entered Commands in the file are not colour coded Save as a.py file

Lesson 5

Activity Where does the name ‘Boolean’ come from? Boolean variables are named after George Boole who invented the mathematics of digital logic. Find a picture of George Boole and insert it here. When was he born? He was born on November 2 nd, 1815 in Lincoln..

Activity Type commands Use the “type” function to find out the data types for these values. Explain the results It tells you what data type they are. Programming screen| Python Shell

Activity 5.3 – Type commands The type function returns the data type of an expression. For each of these expressions, first predict the data type then use the type command to see if your prediction was correct. ExpressionPredicted data typeType commandResult “hello world” type("hello world") string False type(False) boolean 15 type(15) integer 35.6 type(35.6) float -999 type(-999) integer “15” type(“15”) integer “False” type(“False”) string True type(True) boolean type(0.001) float

Activity Variables The interactive shell is a useful place to explore variables. Copy and run this code into python. Create a variable called myName and assign it to your name. Create a variable called myAge and assign it to your age Create a variable called myEyes and assign it to your eye colour. Create a variable called myHeight and assign it to your height. Write the commands to display on the screen your name, age, eye colour and height. Evidence both python shells the next slide Hint: Remember that a single ‘=’ is used to assign a value to a variable.

Activity Variables Explain the results I created the variables and then printed them in a sentence. Programming screenPython Shell

Activity 5.5 – Variable names A programmer is trying to decide on a valid name for a variable that represents a house number. Which of the following variable assignments are valid? Why are the others not valid? Valid or invalid variable name? Reason why not valid 8HouseNumber = 288Invalid Variable names must begin with a letter houseNumber = 288Valid house Number = 288Invalid Variable names cannot have spaces in them house_number = 288Valid “_” is a valid character in a variable name import = 288Invalid Import is a Python command What type of error do you get when using an invalid variable name? Invalid syntax

Activity 5.6- Type commands Copy and run this code and explain the result. Amend the program to add another variable called numberThree. Assign the value 76 to this variable. The program should all up all three numbers and print the result. Evidence both python shells the next slide

Activity Variables Explain the results The program adds the two numbers together and displays the answer. Programming screenPython Shell

Activity 5.7 – Data types in Python Complete this table to describe the four data types. Data type Python abbreviat ion ExplanationExample integerint A whole number 45 stringstr A sequence of characters which can include letters, spaces and other characters ”Have a nice day” float A number with a fractional part booleanbool Boolean or logical data can only have one of two values. In Python they are either True or False True or False Hint: Quotation marks are used to show the start and end point of a string, e.g. “this is a string”.

Lesson 6

Activity 6.1 – Input function The input function allows the user to input a value and assign it to a variable. Copy and run this program. Re-run the program several times, entering different names each time. Hint: Quotation marks are used to show the start and end point of a string, e.g. “this is a string”.

Activity Input function Write a program that asks the user for their name and favourite food and displays these on the screen. Did you have to correct any errors? If so, what were they? Programming screen| Python Shell

Activity 6.2 – Int function The input function allows the user to input a value and assign it to a variable. Copy and run this program. Explain why It doesn't work The program cannot add the number 10 to a string, which is what the user is asked to input in this code.

Activity – Int function Explain the results I Used the int function to convert from a string to an integer if you want to input whole numbers. Programming screen Python Shell Correct the program from the previous page and display below.

Activity 6.3 – Int function Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks you to enter a number then displays the number doubled.

Activity 6.4 – String formatting The string method.format gives you more control over the formatting of your output than printing using space-separated values. The string formatting commands are given in curly brackets {}. Copy the following programs into python and explain what happens and why. CodeWhat happens and why? foodOne="fish" foodTwo="chips" print("{0} and {1}".format(foodOne,foodTwo)) The program prints fish and chips. foodOne="fish" foodTwo="chips” print("{1} and {0}".format(foodOne,foodTwo)) The program prints chips and fish. foodOne="fish" foodTwo="chips" print("{1} {1} {1} and {0} {0} {0}".format(foodOne,foodTwo)) The program prints chips chips chips and fish fish fish.

Activity – String formatting Create these variables: one = “cheese” two =”onion” Use the.format command to display: My favourite crisps are cheese and onion. I love them! cheese and onion and cheese and onion and cheese and onion cheesecheesecheese and oniononiononion You guessed it. The best crisps are onion and … cheese. Display your code on the next slide…

Activity – String formatting Did you have to correct any errors? If so, what were they? The.format method is about formatting strings so the.format fields {0} must be a string. The format field {0} placeholders are replaced (in order) by the values given in.format brackets. The values in the.format brackets are separated by commas. Programming screen Python Shell

Activity 6.5 – Formatting numbers You can use the.format method to format the number of decimal places. Copy the following programs into python add the answer and explain what happens and why. CodeWhat happens and why? number = print("The answer is {0:.5f}".format(number)) The answer is It prints because the field format has requested the number is printed to 5 decimal places. number = print("The answer is {0:.4f}".format(number)) The answer is It prints because the field format has requested the number is printed to 4 decimal places. number = print("The answer is {0:.3f}".format(number)) The answer is It prints because the field format has requested the number is printed to 3 decimal places. number = print("The answer is {0:.1f}".format(number)) The answer is 98.1 It prints 98.1 because the field format has requested the number is printed to 1 decimal places. number = print("The answer is {0:.0f}".format(number)) The answer is 98 It prints 98 because the field format has requested the number is printed to 0 decimal places.

Activity – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Assign the number to a variable and display the number with 5 decimal places using the.format command.

Activity – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Assign the number to a variable and display the number with 2 decimal places using the.format command.

Activity – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Assign the number to a variable and display the number with no decimal places using the.format command.

Activity 6.6 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks how much your bill is at a restaurant and then asks you to enter the % you want to give in a tip. The program should display the amount of tip to give to the waiter.

Activity – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Amend the program so that it also displays the total cost of the meal including the tip.

Activity 6.7 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that displays the square of a number.

Activity – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program to find the perimeter of a rectangle.

Activity – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that finds the area of a square.

Lesson 7

Activity 7.1 – Relational operators Password checking is an example of a relational operator. If the password entered is the same as the password stored then the condition is true. The operator is ‘is equal to’. Brainstorm other examples of condition statements and decide what the operator is. Relation statementOperator Entered password equal to saved passwordEqual to People more than 18 years old can voteGreater than Temperature less than zero degrees water freezes Less than Cost of tickets for a concert greater than £30 I cannot afford to go Greater than Temperature in the room greater than 23 degrees – open the window Greater than Exam result greater than or equal to 75% get an A Greater than or equal to Hint: Greater than is an example of an operator

Activity 7.2 – Relational operators Complete this table of the Python relational operators. Give an example of each and say whether it will evaluate to true or false. Hint: Greater than is an example of an operator Relational operator OperatorExampleEvaluates to Equal to==“fred” == “sid”False Not equal to!=8 != 8False Greater than>10 > 2True Greater than or equal to >=5 >= 5True Less than<40 < 34False Less than or equal to <=2 < = 109True

Activity 7.3 – Greater than and less than Find a way that works for you of remembering the difference between “less than”. Hint: As there are only two options you only need to learn one! Operator>< Operator meaningGreater thanLess than How I remember thisThe number is at the largest part of the operator. The number is at the smallest part of the operator.

Activity 7.4 – Operators Programming screenPython Shell This program asks if it is snowing and if so tells you to take a sledge otherwise to have a good day. The condition is missing. Copy and complete the condition to make the program work correctly.

Activity 7.5 – Operators What condition is needed in this program to display ‘pass’ if the exam mark is 40 or more?

Activity 7.6 – Operators Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks you to enter the colour of the light at a pedestrian crossing. If the light is green it tells you it is safe to cross, otherwise it tells you to stop.

Activity 7.7 – Operators Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks you for your password. It then asks you to re- enter your password. If they are the same the message “access granted” is displayed. If the passwords are not the same the message “access denied” is displayed.

Lesson 8

Activity 8.1– Using elif This program simulates a fortune cookie. A random number is used to decide your ‘fortune’. Copy and run this program. # a random number is given by the randint() function import random answer= random.randint(1,6) if answer == 1: print("You will make a new friend this week") elif answer == 2: print("You will do well in your GCSEs") elif answer == 3: print("You will find something you thought you’d lost") The program is not yet complete. Include your own ‘fortunes’ for the numbers 4, 5 and 6 and evidence this on the next slide.

Activity 8.1.1– Using elif Programming screen Python Shell Include your own ‘fortunes’ for the numbers 4, 5 and 6 and evidence this on the next slide. Note: random.randint(1,6) is a function that returns a random number between 1 and 6. The ‘import random’ command allows the program to access the random.randint() function.

Activity 8.2– Writing readable code: a style guide Program code is read more often that it is written. Here are some guidelines on how to write Python code so that it is easy to read. A style guide for Python code Indent by four spaces for each indentation level. Use blank lines to separate different parts of the program. Use two blank lines to separate functions. Choose meaningful names for variables, using CamelCase or with words separated by underscores. Put imports at the top of the file. Include one space around each side of an assignment and other operators. Use complete sentences with the first word capitalised for comments. Write comments that add clarity and explain what the program does. Do not simply repeat what the code already says. Write function names in lowercase, with words separated by underscores. Use meaningful function names which describe the purpose of the function. Write constants in CAPITAL_LETTERS. Use meaningful constant names, which describe the purpose of the constant.

Activity – Writing readable code Did you have to correct any errors? If so, what were they? def a(s): if s<50: print("You have lost") else: print("You have won") Print screen of amended code using the style guide Apply the rules in the style guide above for this Python code.

Activity – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Amend the program so that it also displays the total cost of the meal including the tip.

Activity 6.7 – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that displays the square of a number.

Activity – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program to find the perimeter of a rectangle.

Activity – Formatting numbers Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that finds the area of a square.

Lesson 7

Activity 7.1 – Relational operators Password checking is an example of a relational operator. If the password entered is the same as the password stored then the condition is true. The operator is ‘is equal to’. Brainstorm other examples of condition statements and decide what the operator is. Relation statementOperator Entered password equal to saved passwordIs equal to People more than 18 years old can voteGreater than Temperature less than zero degrees water freezes Less than Cost of tickets for a concert greater than £30 I cannot afford to go Greater than Temperature in the room greater than 23 degrees – open the window Greater than Exam result greater than or equal to 75% get an A Greater than or is equal to Hint: Greater than is an example of an operator

Activity 7.2 – Relational operators Complete this table of the Python relational operators. Give an example of each and say whether it will evaluate to true or false. Hint: Greater than is an example of an operator Relational operator OperatorExampleEvaluates to Equal to==“fred” == “sid”False Not equal to!=/<>“fred” != “sid”True Greater than>1>2False Greater than or equal to >=2.5 >= 5 / 2True Less than<5<10True Less than or equal to <=6<=10True

Activity 7.3 – Greater than and less than Find a way that works for you of remembering the difference between “less than”. Hint: As there are only two options you only need to learn one! Operator>< Operator meaningGreater thanLess than How I remember thisMouth points to biggest number

Activity 7.4 – Operators Programming screenPython Shell This program asks if it is snowing and if so tells you to take a sledge otherwise to have a good day. The condition is missing. Copy and complete the condition to make the program work correctly. Having two variables here doesn’t always work

Activity 7.5 – Operators What condition is needed in this program to display ‘pass’ if the exam mark is 40 or more? If mark >= 40:

Activity 7.6 – Operators Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks you to enter the colour of the light at a pedestrian crossing. If the light is green it tells you it is safe to cross, otherwise it tells you to stop.

Activity 7.7 – Operators Did you have to correct any errors? If so, what were they? Programming screen Python Shell Write a program that asks you for your password. It then asks you to re- enter your password. If they are the same the message “access granted” is displayed. If the passwords are not the same the message “access denied” is displayed.

Lesson 8

Activity 8.1– Using elif This program simulates a fortune cookie. A random number is used to decide your ‘fortune’. Copy and run this program. # a random number is given by the randint() function import random answer= random.randint(1,6) if answer == 1: print("You will make a new friend this week") elif answer == 2: print("You will do well in your GCSEs") elif answer == 3: print("You will find something you thought you’d lost") The program is not yet complete. Include your own ‘fortunes’ for the numbers 4, 5 and 6 and evidence this on the next slide.

Activity 8.1.1– Using elif Programming screen Python Shell Include your own ‘fortunes’ for the numbers 4, 5 and 6 and evidence this on the next slide. Note: random.randint(1,6) is a function that returns a random number between 1 and 6. The ‘import random’ command allows the program to access the random.randint() function.

Activity 8.2– Writing readable code: a style guide Program code is read more often that it is written. Here are some guidelines on how to write Python code so that it is easy to read. A style guide for Python code Indent by four spaces for each indentation level. Use blank lines to separate different parts of the program. Use two blank lines to separate functions. Choose meaningful names for variables, using CamelCase or with words separated by underscores. Put imports at the top of the file. Include one space around each side of an assignment and other operators. Use complete sentences with the first word capitalised for comments. Write comments that add clarity and explain what the program does. Do not simply repeat what the code already says. Write function names in lowercase, with words separated by underscores. Use meaningful function names which describe the purpose of the function. Write constants in CAPITAL_LETTERS. Use meaningful constant names, which describe the purpose of the constant.

Activity – Operators Did you have to correct any errors? If so, what were they? def a(s): if s<50: print("You have lost") else: print("You have won") Print screen of amended code using the style guide Apply the rules in the style guide above for this Python code.