Day 2 - Reach Cambridge

Download Report

Transcript Day 2 - Reach Cambridge

Week 1, Day 2
Programmatic mastery
28 June 2016
Python Basics:
Day 1 Recap
Recap β™»β™»πŸ’‘πŸ’‘
β€’
β€’
β€’
β€’
β€’
β€’
β€’
β€’
Installing
What is programming?
Arithmetic Operations
Functions
Interpreter vs Script files
The print and input functions
Types
Variables
Conditionals
Day 2 Plan
β€’
β€’
β€’
Conditional Statements
Boolean Logic
Boolean Expressions
Loops
β€’
β€’
β€’
Loop Statements
While Loops
Loop Patterns
Conditional Statements
(AKA if statements AKA branches)
🌡 πŸ”€
Python Symbol
Meaning
==
!=
>
<
Greater than, Less than
<=
Greater than or equals,
Less than or equals
>=
Equals, Not Equals
Python Comparison Operators
Exercise 2.1 – Programmable Calculator
Write a short calculator program that the use can set into
four different modes by answering a suitable prompt.
This program should then prompt the user for two
numbers, and depending upon the mode the calculator is
in, print out the result
Alternatives
Exercise 2.1(b) – Programmable Calculator
Write a short calculator program that the use can set into
four different modes by answering a suitable prompt.
This program should then prompt the user for two
numbers, and depending upon the mode the calculator is
in, print out the result
Use only one if, multiple elifs and an else
Python Symbol
Meaning
and
Both expressions must
or
One expression must
not
The expression must
not be true
be true
be true
Python Logical Operators
Exercise 2.2 - PythonFunQuiz
Write a short program that asks the user five questions
If the user gets the answer correct, print "Correct" and
award them a point.
If their final score is greater than 3 print out a suitable
message of encouragement.
Exercise 2.2(b) - PythonFunQuiz
Write a short program that asks the user five questions
If the user gets the answer correct, print "Correct" and
award them a point.
If their final score is less than 2 points, print out a message
of commiseration, between 3 and 4 points, print out a
suitable message of encouragement. If it’s exactly 5,
congratulate them!
Exercise 2.3 - Mark of Authority
Write a short program that prompts the user to enter the
percentage marks of a hapless student for a series of
subjects.
Perform the necessary calculations to determine if the
student has passed all subjects (their mark in each must be
more than 50%)
Exercise 2.3(b) - Mark of Authority
Write a short program that prompts the user to enter the
percentage marks of a hapless student for a series of
subjects.
Perform the necessary calculations to determine if the
student has passed all subjects (their mark in each must be
more than 50%) as well as determining if they've passed
the whole year (average mark must be more than 50%).
Loops
Exercise 2.4
Look around the room.
Observe the people for a while, and try to spot someone
who is performing the same set of actions repeatedly.
Think about why they are repeating themselves, and make
a prediction about what would make them stop
The While Loop
Exercise 2.5 - Starry Loops
Write a program that:
β€’ Prompts a user to input a size.
β€’ Print out as many *s as specified by the user, each on
its own line
Exercise 2.5(b) - Starry Loops
Write a program that:
β€’ Prompts a user to input a size.
β€’ Print out a pyramid made up of *s that is as tall as
specified by the user
Loop Pattern: Counter Loops
Loop Pattern: Infinite Loops
Loop Pattern: Run-until
Ex 2.6 - Two Calculators walk in...
Write a program that:
β€’ Asks a user how many calculations they would like to
perform.
β€’ Then, for however many calculations, prompt the user
for the operation they would like to perform, as well as
the two numbers involved.
Ex 2.6(b) - Two Calculators walk in...
Modify your calculator so that rather than asking for the
number of calculations, the program runs until the user
indicates that they would like to exit
Conditionals:
Day 2
Recap β™»β™»πŸ’‘πŸ’‘
β€’
β€’
β€’
β€’
Conditional Statements
Alternatives
Boolean Logic
Boolean Expressions
Loops:
β€’ Loop Statements
β€’ While Loops
β€’ Loop Patterns
Now for the Challenges! πŸ’ͺπŸ†πŸ’ͺπŸ†
Ex 2.7 – Interactive Story
Write a short choose-your-adventure story in
programmatic form.
Prompt the user for inputs, and the story progresses based
upon their choices.
Ex 2.8 – Role-Playing Game
Extend your interactive story by making it into a Role
Playing Game. Keep track of attributes such as health,
armour level, etc.
Determine the outcome of scenarios based on the values
of these attributes as well as player choices.