Download presentation
Presentation is loading. Please wait.
1
Clarkson University Google IgniteCS
2
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?
3
Our Topics Intro to Python Intro to Algorithms Intro to Networking
4
First what is computer science?
5
First what is computer science?
The study of meeting people’s needs with computers
6
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
7
So what were the challenges?
8
So what were the challenges?
Programmer could give confusing directions. Computer does not know what the programmers goal is.
9
What are some extra challenges we have with computers?
10
What are some extra challenges we have with computers?
They don’t speak English.
11
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
12
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
13
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
14
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)
15
Let us set up our computers
Remove the cables from the box. Split all items not in the components box evenly.
16
Let us set up our computers
Connect the HDMI cable to the monitor and the other end to the Raspberry Pi.
17
Let us set up our computers
Connect the mouse and keyboard to the USB ports on the Raspberry Pi.
18
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.
19
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.
20
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.
21
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.
22
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
23
Power up your device and let us open Python
Open the program python3(IDLE3)
24
What can this thing do… Calculate the summation of your two favorite numbers
25
What can this thing do… Calculate the summation of your two favorite numbers Calculate the product of your two favorite numbers
26
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
27
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
28
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
29
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 See if your computer can take a product of two gigantic numbers!
30
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
31
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
32
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.
33
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
34
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.
35
Where is the Square Root Key?
There is no square root key so how should we calculate it:
36
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?
37
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
38
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)
39
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
40
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:
41
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
42
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
43
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.
44
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.
45
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
46
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.
47
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 (-b + pow( b*b – 4 * a * c, .5))/(2 * a) Value the computer will calculate to return. Using the parameters we defined in
48
Let’s test it out! We can now debug using the following input:
y= x 2 +60x+5
49
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)
50
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.
51
Using Vim, getting into vim
We can get into vim using the following command: vim Name of the program
52
Using Vim, getting into vim
We can get into vim using the following command: vim myFirstFile Name of the file I would like to open, note: It does not need to exist yet.
53
Using Vim, getting into vim
We can get into vim using the following command: vim myFirstFile.py Tells the computer that this file is in the python language
54
Inside VIM Three modes:
Viewing mode: Mode you enter when you first enter the program, can only view the document, use the arrow keys to navigate Insertion mode Command mode
55
Inside VIM Three modes: Viewing mode
Insertion mode: Enter this mode from viewing mode by pressing the ‘i’ key. In this mode you can change the document like any other document, use the arrow keys to navigate. Leave insertion mode by ‘esc’ key on top left of your keyboard Command mode
56
Inside VIM Three modes: Viewing mode Insertion mode
Command mode: Enter the command mode by typing ‘:’ in viewing mode to run commands. Only a couple commands we will use: ‘w’ : Write/save your work ‘wq’ : Write/save and quit the file ‘q!’ : quit without saving ‘esc’ : return to viewing mode without running a command
57
Creating .py in IDLE Go File -> New File
We then get a new file, before we start lets save Go File -> Save as
58
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
59
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
60
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
61
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
62
So now let us test this Run the program by run -> run module
63
Axis of symmetry radius radius
64
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
65
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
66
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
67
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
68
Sidenote We can return multiple values (called tuples) by:
return val1, val2, val3 We then save these values using: val1, val2, val3 = test()
69
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 23 23 68 1 2 3
70
Retrieving elements in a list
Then to get the element out of each bin we use example The name of the list
71
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
72
Retrieving elements in a list
Then to get the element out of each bin we use example[1] The element we would like.
73
Retrieving elements in a list
Then to get the element out of each bin we use example[1] 32 23 23 68 1 2 3
74
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
75
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
76
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
77
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
78
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…
79
Fixing the error ideologically
We want the following function: If a=0: −𝑐 𝑏 else: −𝑏+ 𝑏 2 −4𝑎𝑐 2𝑎
80
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]
81
Let us code this: If a is 0 the computer will otherwise
Our new If statement: def findRoots (a, b, c): if a == 0: 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 a is 0 the computer will otherwise
82
Let us code this: This statement is a conditional statement
Our new If statement: def findRoots (a, b, c): if a == 0: 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] This statement is a conditional statement
83
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 >=
84
Let us code this: Our new If statement: def findRoots(a, b, c): if a == 0: 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] Keyword says we will do the next block if we don’t do the previous block
85
Another boundary case Now try the call findRoots(0, 0, 3)
We need to make a condition for b equaling 0
86
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 [] return [-c / b] 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
87
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 [] return [-c / b] 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
88
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 [] return [-c / b] 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
89
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 [] return [-c / b] 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
90
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 [] return [-c / b] 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
91
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 [] return [-c / b] 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
92
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 [] return [-c / b] 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
93
Testing time Now test on the following inputs:
𝑦= 𝑥 2 +2𝑥+1 𝑦=5 𝑥 𝑥−4 𝑦= 𝑥 2 𝑦=3 𝑦=−2𝑥+4
94
Defining a new function
Our prompting function def prompt(): We will define a function prompt with no arguments
95
Creating a variable to hold user input
Our prompting function def prompt(): a = It will create a variable a
96
Creating a variable to hold user input
Our prompting function def prompt(): a = float( The value of a will be of type float
97
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
98
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
99
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
100
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
101
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
102
Special Characters in a string
Character sequence Value \\ \ \’ ‘ \n New Line \t Tab
103
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
104
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
105
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
106
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 ________
107
Properties of a for loop
We will add for root in roots: print(‘A root is {0:.3f}.’.format(root)) Keyword for to start a for loop
108
Properties of a for loop
We will add for root in roots: print(‘A root is {0:.3f}.’.format(root)) Creating a variable call root that will sequentially take all the value for each element in root
109
Properties of a for loop
We will add for root in roots: print(‘A root is {0:.3f}.’.format(root)) Keyword in
110
Properties of a for loop
We will add for root in roots: print(‘A root is {0:.3f}.’.format(root)) Specifying the list we will iterate on
111
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
112
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
113
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
114
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.
115
Formatting a string We will add The bracket begins the replacement
for root in roots: print(‘A root is {0:.3f}.’.format(root)) The bracket begins the replacement
116
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
117
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
118
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
119
Formatting a string We will add It is expecting to format a float.
for root in roots: print(‘A root is {0:.3f}.’.format(root)) It is expecting to format a float.
120
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))
121
More Testing Let us test on the following input
− 𝑥 2 +3𝑥+4 4 𝑥 2 +5𝑥−3
122
Let us try a more complex polynomial
Now let us try 𝑥 2 +1
123
Let us try a linear equation
𝑥=1
124
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
125
Checking if we have a boundary case
The new addition of code if Beginning of the if statement.
126
Checking if we have a boundary case
The new addition of code if len(roots) == 0: The length of the list roots.
127
Checking if we have a boundary case
The new addition of code if len(roots) == 0: Equals 0
128
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.’
129
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))
130
Let us try these Now let us try to solve 0=3 0=0
131
Infinite case When we have infinite solutions we want to print:
There are infinite solutions.
132
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’) 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
133
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.
134
More Testing Test by solving 0=0
135
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.
136
A new function This new function will be called run def run():
The function run will have no parameters
137
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
138
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
139
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’
140
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
141
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’
142
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.
143
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: ‘)
144
MORE TESTING! Let us test our new function.
145
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.
146
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 We add an infinite loop with the following body
147
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 First ask the user for their input just as before
148
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
149
Making our computer talk
Let us make this so that the raspberry pi talks to us. We do not know how to directly do this but I bet someone else has figured it out We are going to use both what is in the operating system and import other code to make our code a lot better.
150
Importing other programs
We will add the following line to the top of our file to tell the computer to import other files import os, time
151
Running other program We will now make a function that makes the operating system speak a string, and print it out to the operating system.
152
Creating a function to make the computer talk
Our speak function def speak(str): os.system(‘espeak \‘ ‘ + str + ‘ \‘ ‘) The function speak will have one parameter, our string
153
Running a command Our speak function def speak(str): os.system(‘espeak \‘ ‘ + str + ‘ \‘ ‘) First we call the os, to run a command, which is passed as a string
154
Running a command We are running the program espeak on the string, str
Our speak function def speak(str): os.system(‘espeak \‘ ‘ + str + ‘ \‘ ‘) We are running the program espeak on the string, str
155
Running a command We then make the computer speak by adding the command speak, with the same parameters, before all our print and input function calls.
156
Free Resources If you wanted to continue your studies try these three free resources:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.