Clarkson University
Google IgniteCS
Have you ever wanted…
• To know how to make a computer do tasks for you?
• To learn the basics of how google works?
• Have a secret way to talk to your friends?
Our Topics
• Intro to Python
• Intro to Algorithms
• Intro to Networking
First what is computer science?
First what is computer science?
• The study of meeting people’s needs with computers
First without computers…
• Partner up, tell each other your:
•
•
•
Name.
Tell them one fun fact.
Favorite flavor of ice cream and why.
• We are going to lead our partner through a maze.
•
•
One of you will be our computer
One of you will be our programmer
So what were the challenges?
So what were the challenges?
• Programmer could give confusing directions.
• Computer does not know what the programmers goal is.
What are some extra challenges we have with
computers?
What are some extra challenges we have with
computers?
• They don’t speak English.
The way we will program.
• Our computer: Raspberry PI 3
•
•
Very affordable
Easy to use
• Our language with the Computer: Python
•
•
Very powerful language and easy to use
We can find online help very easily
First a little business:
• Reasons to choose a computer
•
So you can buy one for yourselves if you are interested in pursuing some of these
projects further
Reminders before the fun.
• These are fragile computers. General things to follow:
•
•
Please handle all boards along their edges
Please only handle something when explicitly told to do so
Now fun
•
Your kit should include:
•
•
•
•
•
•
•
•
•
Two Raspberry Pi 3’s
Two Screens
Two Screen Power Supplies
Four MicroUSB cables
A USB Hub (optional)
Two HDMI Cables
Two Ethernet Cables
Two USB Power Adapters
Another box of fun (Leave that for later)
Let us set up our computers
•
Remove the cables from the box. Split all items
not in the components box evenly.
Let us set up our computers
•
Connect the HDMI cable to the monitor and
the other end to the Raspberry Pi.
Let us set up our computers
•
Connect the mouse and keyboard to the USB
ports on the Raspberry Pi.
Let us set up our computers
.
•
Open the components box, and take out the
two MicroSD cards, and carefully place them in
the MicroSD slots in the bottom of the
Raspberry Pi. Be VERY delicate with them. The
top of the SD card should face down to the
table.
Let us set up our computers
•
Connect the Screen Power Cable to the power
strip at the center of your table. Please do not
place the monitor on the board.
Let us set up our computers
•
Connect the Raspberry Pi’s MicroUSB cable to
the USB Power Supply, and plug the USB
power supply into the power strip at the center
of your table.
Let us set up our computers
• Please wait about 30 seconds for your Raspberry Pi to boot up. If it does not
boot, please raise your hand now, and someone will come over and help you
fix the problem.
Our Computer’s Operating System
• Our computer runs Rasbian, a operating system based off of Linux
•
•
Linux is open source: meaning the code is open to anyone to change
Windows and Apple are both proprietary operating systems
Power up your device and let us open Python
• Open the program python3(IDLE3)
What can this thing do…
•
Calculate the summation of your two favorite
numbers
What can this thing do…
•
•
Calculate the summation of your two favorite
numbers
Calculate the product of your two favorite
numbers
What can this thing do…
•
•
•
Calculate the summation of your two favorite
numbers
Calculate the product of your two favorite
numbers
Calculate the quotient of your two favorite
numbers
What can this thing do…
•
•
•
Calculate the summation of your two favorite
numbers
Calculate the product of your two favorite
numbers
Calculate the quotient of your two favorite
numbers
What can this thing do…
•
•
•
•
Calculate the summation of your two favorite
numbers
Calculate the product of your two favorite
numbers
Calculate the quotient of your two favorite
numbers
See if your computer has the infamous answer
to 10
What can this thing do…
•
•
•
•
•
Calculate the summation of your two favorite
numbers
Calculate the product of your two favorite
numbers
Calculate the quotient of your two favorite
numbers
See if your computer has the infamous answer
to 10
See if your computer can take a product of two
gigantic numbers!
Wow you just showed me the world’s most
bulky calculator, what else can I do…
•
Let us define variables…
•
Let us define our favorite numbers as our
name. Example:
Ryan = 3
•
•
Now calculate the summation, product and
quotient of your numbers using variables.
Try to change your favorite number
Ryan = 4
Wow you just showed me the world’s most
bulky calculator, what else can I do…
•
Let us define variables…
•
Let us define our favorite numbers as our
name. Example:
Ryan = 3
Wow you just showed me the world’s most
bulky calculator, what else can I do…
•
Let us define variables…
•
Let us define our favorite numbers as our
name. Example:
Ryan = 3
•
Now calculate the summation, product and
quotient of your numbers using variables.
Wow you just showed me the world’s most
bulky calculator, what else can I do…
•
Let us define variables…
•
Let us define our favorite numbers as our
name. Example:
Ryan = 3
•
•
Now calculate the summation, product and
quotient of your numbers using variables.
Try to change your favorite number
Ryan = 4
More complicated Functions.
Ok so now let’s do something a little more
interesting: Calculate Quadratic Equation. Recall,
when we have
𝑦 𝑥 = 𝑎𝑥 2 + 𝑏𝑥 + 𝑐
the roots of the equation are at
𝑥=
−𝑏 ± 𝑏2 −4𝑎𝑐
.
2𝑎
Let us use Python get the larger root of
𝑦 = 𝑥 2 + 60𝑥 + 5.
Where is the Square Root Key?
•
There is no square root key so how should we
calculate it:
Where is the Square Root Key?
•
There is no square root key so how should we
calculate it:
•
Use the keys we currently have and calculate
square root another way, possibly newton’s
method?
Where is the Square Root Key?
•
There is no square root key so how should we
calculate it:
•
•
Use the keys we currently have and calculate
square root another way, possibly newton’s
method?
Maybe someone else has had this problem, so
python must have solved it
Where is the Square Root Key?
•
There is no square root key so how should we
calculate it:
•
•
•
Use the keys we currently have and calculate
square root another way, possibly newton’s
method?
Maybe someone else has had this problem, so
python must have solved it
Function calls: we will use
pow(base, power)
Ok that is useful, but I don’t want to type this
in every time I want to get a root.
•
•
You can redefine your variables and use the up
arrow to recall a command
Let’s race to calculate the following:
𝑦 = 𝑥 2 + 2𝑥 + 3
𝑦 = 6𝑥 2 + 21𝑥 + 4
𝑦 = −2𝑥 2 − 2
Wow that is still a little too much work, I feel
really lazy
•
Let us define a function! We can call it “find
upper root”. Defining a function:
Wow that is still a little too much work, I feel
really lazy
•
Let us define a function! We can call it “find
upper root”. Defining a function:
def
Keyword that defines a function
Wow that is still a little too much work, I feel
really lazy
•
Let us define a function! We can call it “find
upper root”. Defining a function:
def findUpperRoot
Name of my function, note:
No spaces, and starts with letters
Wow that is still a little too much work, I feel
really lazy
•
Let us define a function! We can call it “find
upper root”. Defining a function:
def findUpperRoot(a, b, c)
My Parameters, I want to give my
function the values of a, b, and c.
Note: between parenthesis and
comma separated.
Wow that is still a little too much work, I feel
really lazy
•
Let us define a function! We can call it “find
upper root”. Defining a function:
def findUpperRoot(a, b, c):
Special Character to say that,
what I want findUpperRoot to do
follows.
Wow that is still a little too much work, I feel
really lazy
•
Let us define a function! We can call it “find
upper root”. Defining a function:
def findUpperRoot(a, b, c):
return
Tab to tell the computer that this
line is in the function
Wow that is still a little too much work, I feel
really lazy
•
Let us define a function! We can call it “find
upper root”. Defining a function:
def findUpperRoot(a, b, c):
return
Keyword that tells the function
that it is giving the following value
back to me.
Wow that is still a little too much work, I feel
really lazy
•
Let us define a function! We can call it
“find upper root”. Defining a function:
Value the computer will
calculate to return.
Using the parameters
we defined in
def findUpperRoot(a, b, c):
return (-b + pow( b*b – 4 * a * c, .5))/(2 * a)
Let’s test it out!
•
We can now debug using the following input:
y = x 2 + 60x + 5
I made this cool function but… is it saved?
•
•
•
Type quit() to quit from python.
Enter python again by the python command
Try your function call again by
findUpperRoots(1, 60, 5)
Do you mean I have to this every time I want
code!
•
•
No, we will write a .py file, which is essentially
a memory of what commands we wrote.
We will use a program called idle to write make
this file.
Creating .py in IDLE
•
•
•
Go File -> New File
We then get a new file, before we start lets
save
Go File -> Save as
Our first line of every program
•
At the top of every python program we should
add the following line:
#!/usr/bin/env python3
This tells the computer that it is
operating system level
Our first line of every program
•
At the top of every python program we should
add the following line:
#!/usr/bin/env python3
Location of the python program
that interprets our python
language
Our first line of every program
•
At the top of every python program we should
add the following line:
#!/usr/bin/env python3
Name of the python program, we
are looking for python 3, another
common one is python 2
Ok so now let us make this function
•
So now in vim add the function as follows:
def findUpperRoot(a, b, c):
return (-b + pow( b*b – 4 * a ...
* c, .5))/(2 * a)
Tells the computer that the next
line is a continuation of this line
So now let us test this
•
Run the program by run -> run module
Axis of
symmetry
radius
radius
Let us make this a little more fancy
•
Let us define a variable inside of the function,
make the following change
def findUpperRoot(a, b, c):
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return axis + radius
Let us make this a little more fancy
•
Let us define a variable inside of the function,
make the following change
def findUpperRoot(a, b, c):
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return axis + radius
Note: Tabs before all the lines in
the function
Let us make this a little more fancy
•
Let us define a variable inside of the function,
make the following change
def findUpperRoot(a, b, c):
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return axis + radius
Defining the variables, note that
we can use the variables in the
lines following the declaration
Let us make this a little more fancy
•
Let us define a variable inside of the function,
make the following change
def findUpperRoot(a, b, c):
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return axis + radius
Returns the value of val
Sidenote
•
We can return multiple values (called tuples)
by:
return val1, val2, val3
•
We then save these values using:
val1, val2, val3 = test()
Now we have an upper root, we want both
roots.
•
We can return a list, an ordered sequence of
numbers. Each number goes into a indexed
bin.
example = [32, 23, 23, 68]
32
0
23
1
23
2
68
3
Retrieving elements in a list
•
Then to get the element out of each bin we use
example
The name of the list
Retrieving elements in a list
•
Then to get the element out of each bin we use
example[ ]
Brackets to denote that we want to get an
element
Retrieving elements in a list
•
Then to get the element out of each bin we use
example[1]
The element we would like.
Retrieving elements in a list
•
Then to get the element out of each bin we use
example[1]
32
0
23
1
23
2
68
3
So now let us make the code return a list
instead of a single element
def findRoots(a, b, c):
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis – radius, axis + radius]
Brackets to denote that we want to return a
list
So now let us make the code return a list
instead of a single element
def findRoots(a, b, c):
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis – radius, axis + radius]
Value of our 0th element
So now let us make the code return a list
instead of a single element
def findRoots(a, b, c):
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis – radius, axis + radius]
Comma to delimit we will have another
element
So now let us make the code return a list
instead of a single element
def findRoots(a, b, c):
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis – radius, axis + radius]
Value of our one-th element
Boundary Case
•
•
•
Let us try the following command to test our
function
findRoots(0, 1, 1)
This would be to find the root for
y=𝑥+1
We get an error…
Fixing the error ideologically
•
We want the following function:
If a = 0:
−𝑐
𝑏
else:
−𝑏+ 𝑏2 −4𝑎𝑐
2𝑎
Let us code this:
•
Original function:
def findRoots(a, b, c):
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis – radius, axis + radius]
Let us code this:
•
Our new If statement:
def findRoots (a, b, c):
if a == 0:
return [–c /
If a is 0 the computer will
otherwise
b]
else:
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis – radius, axis + radius]
Let us code this:
•
Our new If statement:
def findRoots (a, b, c):
if a == 0:
This statement is a
conditional statement
return [-c / b]
else:
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis – radius, axis + radius]
Other conditional statements
Syntax
What is does
==
Checks if the values are
equal
<
If the first value is less
than the second
>
Likewise
<=
If the first value is less
than or equal the
second
>=
Likewise
Let us code this:
•
Our new If statement:
def findRoots(a, b, c):
if a == 0:
return [-c / b]
else:
Keyword says we will do
the next block if we don’t
do the previous block
discrim = b*b – 4 * a * c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis – radius, axis + radius]
Another boundary case
•
Now try the call
findRoots(0, 0, 3)
•
We need to make a condition for b equaling 0
We can account for this with another if
statement
def findRoots(a, b, c):
if a == 0:
if b == 0:
if c == 0:
return [float("inf")]
else:
return []
else:
return [-c / b]
else:
discrim = b*b - 4*a*c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis - radius, axis + radius]
The code to handle b being 0
The actions of a nested if
def findRoots(a, b, c):
if a == 0:
if b == 0:
if c == 0:
return [float("inf")]
else:
return []
else:
return [-c / b]
else:
discrim = b*b - 4*a*c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis - radius, axis + radius]
If b is 0 the computer will
otherwise
The actions of a nested if
def findRoots(a, b, c):
if a == 0:
if b == 0:
if c == 0:
return [float("inf")]
else:
return []
else:
return [-c / b]
else:
discrim = b*b - 4*a*c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis - radius, axis + radius]
If c is 0 the computer will
otherwise
The actions of a nested if
def findRoots(a, b, c):
if a == 0:
if b == 0:
if c == 0:
return [float("inf")]
else:
return []
else:
return [-c / b]
else:
discrim = b*b - 4*a*c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis - radius, axis + radius]
The return value is a list
of one element
The actions of a nested if
def findRoots(a, b, c):
if a == 0:
if b == 0:
if c == 0:
return [float("inf")]
else:
return []
else:
return [-c / b]
else:
discrim = b*b - 4*a*c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis - radius, axis + radius]
The value of the element
is of type float
The actions of a nested if
def findRoots(a, b, c):
if a == 0:
if b == 0:
if c == 0:
return [float("inf")]
else:
return []
else:
return [-c / b]
else:
discrim = b*b - 4*a*c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis - radius, axis + radius]
The value of the element
is infinity
The actions of a nested if
def findRoots(a, b, c):
if a == 0:
if b == 0:
if c == 0:
return [float("inf")]
else:
return []
else:
return [-c / b]
else:
discrim = b*b - 4*a*c
denom = 2 * a
axis = -b / denom
radius = pow(discrim, .5) / denom
return [axis - radius, axis + radius]
The return value is an
empty list
Testing time
•
Now test on the following inputs:
𝑦 = 𝑥 2 + 2𝑥 + 1
𝑦 = 5𝑥 2 + 123𝑥 − 4
𝑦 = 𝑥2
𝑦=3
𝑦 = −2𝑥 + 4
Defining a new function
•
Our prompting function
def prompt():
We will define a function
prompt with no arguments
Creating a variable to hold user input
•
Our prompting function
def prompt():
a =
It will create a variable a
Creating a variable to hold user input
•
Our prompting function
def prompt():
a = float(
The value of a will be of
type float
Creating a variable to hold user input
•
Our prompting function
def prompt():
a = float(input(
The value of a will be an
input from the console
Creating a variable to hold user input
•
Our prompting function
def prompt():
a = float(input(‘Number of x squared\’s:’))
The user will be prompted
with the string
Number of x
squared’s
Creating a variable to hold user input
•
Our prompting function
def prompt():
a = float(input(‘Number of x squared\’s:’))
The apostrophe will denote
the beginning of the string
Creating a variable to hold user input
•
Our prompting function
def prompt():
a = float(input(‘Number of x squared\’s:’))
The value of the string is
inside
Creating a variable to hold user input
•
Our prompting function
def prompt():
a = float(input(‘Number of x squared\’s:’))
The \ character is a special character
telling the computer we want a ‘
character and don’t want to end the
string
Special Characters in a string
Character sequence
Value
\\
\
\’
‘
\n
New Line
\t
Tab
Creating a variable to hold user input
•
Our prompting function
def prompt():
a = float(input(‘Number of x squared\’s: ’))
b = float(input(‘Number of x\’s: ’))
c = float(input(‘Number of one\’s: ’))
We will do likewise for the values of
a, b and c
Calculating our roots
•
Our prompting function
def prompt():
a = float(input(‘Number of x squared\’s: ’))
b = float(input(‘Number of x\’s: ’))
c = float(input(‘Number of one\’s: ’))
roots = findRoots(a, b, c)
We will call our function to find our
roots, and store the list as roots
Printing our results to the user
•
Our prompting function
def prompt():
a = float(input(‘Number of x squared\’s: ’))
b = float(input(‘Number of x\’s: ’))
c = float(input(‘Number of one\’s: ’))
roots = findRoots(a, b, c)
print(roots)
Finally print the value of the roots
we found
Let us make it look a little better
•
•
Let’s make the output for each root a little
nicer looking
Let us make it so that for each root we print
A root is ________
Properties of a for loop
We will add
Keyword for to start a for loop
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
Properties of a for loop
We will add
Creating a variable call root that will
sequentially take all the value for
each element in root
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
Properties of a for loop
We will add
Keyword in
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
Properties of a for loop
We will add
Specifying the list we will
iterate on
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
Properties of a for loop
We will add
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
Body of the for loop
Properties of a for loop
We will add
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
We are going to print
Properties of a for loop
We will add
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
The string we are going
to print
Formatting a string
We will add
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
We are going to format
the string, by putting
root into it.
Formatting a string
We will add
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
The bracket begins the
replacement
Formatting a string
We will add
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
The 0th parameter passed
into the format method
will be placed here
Formatting a string
We will add
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
The colon begins the
format part of the
replacement
Formatting a string
We will add
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
The format is, any number of numbers in
front of the decimal point, and at most 3
after the decimal point
Formatting a string
We will add
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
It is expecting to format a float.
Final Product
•
Our new function:
def prompt():
a = float(input(‘Number of x squared\’s: ’))
b = float(input(‘Number of x\’s: ’))
c = float(input(‘Number of one\’s: ’))
roots = findRoots(a, b, c)
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
More Testing
•
Let us test on the following input
−𝑥 2 + 3𝑥 + 4
4𝑥 2 + 5𝑥 − 3
Let us try a more complex polynomial
•
Now let us try
𝑥2 + 1
Let us try a linear equation
•
Let us try
𝑥=1
Let us make the boundary cases have better
print outs
•
Let us print,
There are no roots.
If there are no roots, that is the size of roots is 0
Checking if we have a boundary case
•
The new addition of code
if
Beginning of the if statement.
Checking if we have a boundary case
•
The new addition of code
if len(roots) == 0:
The length of the list roots.
Checking if we have a boundary case
•
The new addition of code
if len(roots) == 0:
Equals 0
Printing we have no roots
•
The new addition of code
if len(roots) == 0:
print(‘There are no roots.’)
We will print ‘There are no roots.’
Our new function
•
Our whole function now:
def prompt():
a = float(input(‘Number of x squared\’s: ’))
b = float(input(‘Number of x\’s: ’))
c = float(input(‘Number of one\’s: ’))
roots = findRoots(a, b, c)
if len(roots) == 0:
print(‘There are no roots’)
else:
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
Let us try these
•
Now let us try to solve
0=3
0=0
Infinite case
•
When we have infinite solutions we want to
print:
There are infinite solutions.
Checking and printing infinite case
•
We could make the change
if len(roots) == 0:
print(‘There are no roots’)
else:
if roots[0] == float(‘inf’):
print(‘There are infinite solutions’)
else:
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
If the roots 0th element is infinity,
we print ‘there are infinite roots
Checking and printing infinite case
•
But we can make this simpler
if len(roots) == 0:
print(‘There are no roots’)
elif roots[0] == float(‘inf’):
print(‘There are infinite solutions’)
else:
for root in roots:
print(‘A root is {0:.3f}.’.format(root))
The elif keyword is equivalent to an
if statement in an else statement.
More Testing
•
Test by solving
0=0
I wish we could have more math
•
Let us now make this program prompt for
more equations while the user still has more
things to enter. We will make a new method.
A new function
•
This new function will be called run
def run():
The function run will have no
parameters
Creating a flag
•
This new function will be called run
def run():
c
We will define a variable c that will
represent the character the user
enters
Checking if the user wants to run again
•
This new function will be called run
def run():
c
while c == ‘y’:
The keyword while will start a while
loop, which runs the body while the
conditional is true
Initializing our flag
•
This new function will be called run
def run():
c = ‘y’
while c == ‘y’:
We want to evaluate the loop atleast
once so we will initialize c as ‘y’
Prompting the user
•
This new function will be called run
def run():
c = ‘y’
while c == ‘y’:
prompt()
We first want to prompt the user for
an equation and solve it
Asking the user if they would like to go again
•
This new function will be called run
def run():
c = ‘y’
while c == ‘y’:
prompt()
c = input(‘Would you like to\
enter another input? Enter y or n: ‘)
Then we want to ask the user if they
would like to enter another equation.
Setting c to ‘y’ or ‘n’
This line is pretty long…
•
This new function will be called run
def run():
c = ‘y’
while c == ‘y’:
prompt()
c = input(‘Would you like to\
enter another input? Enter y or n: ‘)
Notice that the single line of code is
on two lines. Use the ‘\’ to line break a
long line of code.
Our new function
•
This new function will be called run
def run():
c = ‘y’
while c == ‘y’:
prompt()
c = input(‘Would you like to\
enter another input? Enter y or n: ‘)
MORE TESTING!
•
Let us test our new function.
What if we have a cheeky user
•
Note that we do not check to make sure the
user only enter y or n. Let us continue asking
until the user does that.
A new form of while loop
•
This new function will be called run
def run():
We add an infinite loop
with the following body
c = ‘y’
while c == ‘y’:
prompt()
while True:
c = input(‘Would you like to\
enter another input? Enter y or n: ‘)
if c == ‘y’ or c == ‘n’:
break
A new form of while loop
•
This new function will be called run
def run():
First ask the user for their
input just as before
c = ‘y’
while c == ‘y’:
prompt()
while True:
c = input(‘Would you like to\
enter another input? Enter y or n: ‘)
if c == ‘y’ or c == ‘n’:
break
A new form of while loop
•
This new function will be called run
def run():
c = ‘y’
while c == ‘y’:
prompt()
while True:
c = input(‘Would you like to\
enter another input? Enter y or n: ‘)
if c == ‘y’ or c == ‘n’:
break
Then if c equals y or n, we break out of infinite
loop using the break keyword
Free Resources
•
If you wanted to continue your studies try
these three free resources:
https://www.codecademy.com/
https://docs.python.org/3/tutorial/index.html
https://www.raspberrypi.org/resources/
https://repl.it/
https://www.python.org/
© Copyright 2026 Paperzz