SlideShare ist ein Scribd-Unternehmen logo
1 von 11
ISTA 130: Lab 2
1 Turtle Review
Here are all of the turtle functions we have utilized so far in
this course:
turtle.forward(distance) – Moves the turtle forward in the
direction it is currently facing the distance
entered
turtle.backward(distance) – Same as forward but it moves in the
opposite direction the turtle is facing
turtle.right(degrees) – Roates the turtle to the right by the
degrees enteres
turtle.left(degrees) – Same as right, but it rotates the turtle to
the left
turtle.pensize(size) – Adjusts the size of the line left by the
turtle to whatever value is entered for size
turtle.home() – Moves the turtle to the default location and
faces it to the right
turtle.clear() – Clears all the lines that were left by the turtle in
the window.
turtle.penup() – Causes the turtle to stop leaving lines (until pen
is placed back down)
turtle.pendown() – Places the pen back down to the turtle can
continue leaving lines when forward and
backward are called.
turtle.pencolor(color string) – Changes the color of the lines left
by the turtle to whatever color string
entered (so long as Python recognizes it).
turtle.bgcolor(color string) – Changes the background color for
the window that the turtle draws in.
turtle.speed(new speed) – Changes the speed at which the turtle
moves to whatever newSpeed is.
turtle.clearscreen() – Deletes all drawings and turtles from the
screen, leaving it in its initial state
Note that abbreviations also exist for many of these functions;
for example:
� turtle.fd(distance)
� turtle.rt(degrees)
� turtle.pu()
1
2 Functions and Parameters
Here is the square function we looked at yesterday:
def square(side_length):
’’’
Draws a square given a numerical side_length
’’’
turtle.forward(side_length)
turtle.right(90)
turtle.forward(side_length)
turtle.right(90)
turtle.forward(side_length)
turtle.right(90)
turtle.forward(side_length)
turtle.right(90)
return
square(50) # This would give side_length the value of 50
square(100) # This would give side_length the value of 100
print side_length # This will give an error because side_length
# only exists inside the function!
Try it out:
(1 pt.) Create a new file called lab02.py. In this file, create a
simple function called rhombus. It
will take one parameter, side length. Using this parameter, have
your function create a rhombus
using turtle graphics. Call your rhombus function in the script.
What happens if you provide no
arguments to the function? Two or three arguments?
Then, modify your rhombus function so it takes another
argument for the angle inside the
rhombus.
3 Data types
Python recognizes many different types of values when working
with data. These can be numbers,
strings of characters, or even user defined objects. For the time
being, however, were only going to
focus on three of the data types:
integer – These are whole numbers, both positive and negative.
Examples are 5000, 0, and -25
float – These are numbers that are followed by a decimal point.
Examples are 26.58, 0.0, and -1.23
string – These are a string of characters that include letters,
punctuation, symbols, and “white-
space” characters. They are always surrounded by double quotes
(”) or single quotes (’).
Examples are “Hello World!”, “[email protected]#%#!” and “I
like the number 47”.
2
It is important to know what type of values you are going to be
working with when writing
code in Python. Let’s take our square example from above. We
know what happens if we call the
square function using an integer, but what happens if we call it
with a float or string?
Try it out:
(1 pt.) Copy this code for the square function above into your
lab02.py. In the main function,
try calling this function and passing in an integer. Next, try to
pass in a float (just replace the
integer value with a float, no need to call the function twice).
Finally, try calling it by passing it a
string value. Now that youve seen the results of passing it a
string, what happens if you pass the
square function the string “100”?
3.1 Operators and data types
In class we learned some useful operators:
+ addition
- subtraction
* multiplication
** power/exponent function
/ division
// integer division
How do these operators apply to different data types?
Try it out:
In your lab02.py, try each of these 6 operators on every
combination of int, float, and string, and
write down in a comment whether the result is an integer, a
float, a string, or an error. Also, take
note if anything unexpected happens.
There should be six combinations for each operator (int & int,
int & float, int & string, float &
float, float & string, string & string), so you should have 36
lines of code at the end of this!
Do you see any patterns? Is there anything unexpected?
(2 pt.)
3.2 Converting between data types
Copy this program into a new, temporary script, and try it out.
What do you expect it to do?
What does it do?
print "Enter first number: "
num1 = raw_input()
print "Enter second number: "
num2 = raw_input()
numsum = num1+num2
print "The sum is:" + numsum
3
Now try this program. What do you expect it to do? What does
it do?
print "Enter a number: "
num1 = raw_input()
doublenum = num1 / 2
print "Half that number is:" + doublenum
And finally, try this program. What do you expect it to do?
What does it do?
print "This one should be easy."
four = 2 + 2
print "2 + 2 = " + four
The problem: all three of these programs are mixing data types!
Specifically, they’re trying to
combine strings and integers. When a user enters something for
raw input, the result is a string,
not a number.
Fortunately, Python is able to convert between data types, using
these three functions:
int() – convert the argument to an integer
str() – convert the argument to a string
float() – convert the argument to a float (decimal) number
Try this example code to get an idea of how these functions
work:
x = 5
y = ’13’
print x + int(y)
print str(x) + y
Try it out:
(3 pt.) Fix all three of the short programs above so they have
the desired behavior, and copy them
into your lab02.py.
4 For loops
Functions are useful if we know we want to run some code a
small amount of times. What happens
if we want to run it thirty times? A hundred times? If that
happens, it would be a huge hassle
and waste of time to have to type out the function call thirty or
a hundred times. We can use for
loops to easily run code a specific number of times.
Example:
for i in range(<number of times to repeat>):
<code goes here>
4
Using these loops, we only have to change the number in the
range() function to alter the
number of times the code gets repeated, instead of having to
type out or delete the code each time.
For instance, if we wanted to create a lot of triangles next to
each other, we could create a triangle
function to use in a for loop.
Example:
for i in range(30):
triangle(50)
turtle.forward(50)
What happens if you change the 30 in the range function to
other values positive integer values?
What about 0 or negative values? Floats?
Try it out:
(3 pt.) Create a “polygon” function that draws a polygon of any
size, with any number of sides.
For example, the command “polygon(5, 11)” should draw a
hendecagon with sides of length 5.
You may need to remember a bit of geometry for this; try
making a triangle function, a
square function, a pentagon function, and see if you find a
pattern. You’ll also need to use a for
loop in your code, since we don’t know how many sides the
polygon will have until we run the code.
Then, modify your script so that it asks the user for a size and
number of sides, then draws the
requested polygon.
5 What to turn in
When you’re finished, submit your lab02.py file to the Lab 2
dropbox on D2L.
Total Points: 10 points possible
5
Turtle ReviewFunctions and ParametersData typesOperators and
data typesConverting between data typesFor loopsWhat to turn
in
ISTA 130: Homework 2
Due June 5 before class
1 Debugging
Today’s homework deals with debugging. This is the process
that occurs when you find out your
code doesn’t work, and you try to figure out why.
Download the file errors.py from D2L. This file contains a
script with a number of errors in
it. Some are programming language errors, which Python will
report with error messages. Others
are logic errors – code that runs, but does not do what the
programmer intended.
Your task is threefold:
1. Determine which lines have an error
2. In a comment after the line, write down the type of error
message Python gives (if any), and
a brief description of why this line has a problem
3. Correct the error so that the program has the intended
behavior
For example, if the line of code is:
99bottles = 5
Python will give the error:
File "<stdin>", line 1
99bottles = 5
^
SyntaxError: invalid syntax
so the corrected line in your code should look like:
ninetyninebottles = 5 # SyntaxError: variable names can’t start
with a number
If there are multiple errors of the same type in a row, you don’t
have to put the same comment
for all of them; just comment the first and fix the rest.
The code has 15 problems by my count; each will be worth 1
point (15 pt.). Any additional
improvements to the code may be worth extra points (2 pt.).
2 What to turn in
Submit your corrected errors.py script to the Homework 2
Dropbox folder in D2L:
Total Points: 15 points possible
Extra Credit: 2 points possible
1

Weitere ähnliche Inhalte

Ähnlich wie ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx

Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methodsPranavSB
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxSahajShrimal1
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptxNileshBorkar12
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfdata2businessinsight
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golangwww.ixxo.io
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answerskavinilavuG
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answersRojaPriya
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : PythonRaghu Kumar
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Kurmendra Singh
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Fariz Darari
 

Ähnlich wie ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx (20)

Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptx
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdf
 
Python basics
Python basicsPython basics
Python basics
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golang
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : Python
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Pythonintro
PythonintroPythonintro
Pythonintro
 
Maxbox starter
Maxbox starterMaxbox starter
Maxbox starter
 
Python Math Concepts Book
Python Math Concepts BookPython Math Concepts Book
Python Math Concepts Book
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
 

Mehr von priestmanmable

9©iStockphotoThinkstockPlanning for Material and Reso.docx
9©iStockphotoThinkstockPlanning for Material and Reso.docx9©iStockphotoThinkstockPlanning for Material and Reso.docx
9©iStockphotoThinkstockPlanning for Material and Reso.docxpriestmanmable
 
a 12 page paper on how individuals of color would be a more dominant.docx
a 12 page paper on how individuals of color would be a more dominant.docxa 12 page paper on how individuals of color would be a more dominant.docx
a 12 page paper on how individuals of color would be a more dominant.docxpriestmanmable
 
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docxpriestmanmable
 
92 Academic Journal Article Critique  Help with Journal Ar.docx
92 Academic Journal Article Critique  Help with Journal Ar.docx92 Academic Journal Article Critique  Help with Journal Ar.docx
92 Academic Journal Article Critique  Help with Journal Ar.docxpriestmanmable
 
A ) Society perspective90 year old female, Mrs. Ruth, from h.docx
A ) Society perspective90 year old female, Mrs. Ruth, from h.docxA ) Society perspective90 year old female, Mrs. Ruth, from h.docx
A ) Society perspective90 year old female, Mrs. Ruth, from h.docxpriestmanmable
 
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docxpriestmanmable
 
9 AssignmentAssignment Typologies of Sexual AssaultsT.docx
9 AssignmentAssignment Typologies of Sexual AssaultsT.docx9 AssignmentAssignment Typologies of Sexual AssaultsT.docx
9 AssignmentAssignment Typologies of Sexual AssaultsT.docxpriestmanmable
 
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docxpriestmanmable
 
900 BritishJournalofNursing,2013,Vol22,No15©2.docx
900 BritishJournalofNursing,2013,Vol22,No15©2.docx900 BritishJournalofNursing,2013,Vol22,No15©2.docx
900 BritishJournalofNursing,2013,Vol22,No15©2.docxpriestmanmable
 
9 Augustine Confessions (selections) Augustine of Hi.docx
9 Augustine Confessions (selections) Augustine of Hi.docx9 Augustine Confessions (selections) Augustine of Hi.docx
9 Augustine Confessions (selections) Augustine of Hi.docxpriestmanmable
 
8.3 Intercultural CommunicationLearning Objectives1. Define in.docx
8.3 Intercultural CommunicationLearning Objectives1. Define in.docx8.3 Intercultural CommunicationLearning Objectives1. Define in.docx
8.3 Intercultural CommunicationLearning Objectives1. Define in.docxpriestmanmable
 
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docxpriestmanmable
 
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docxpriestmanmable
 
800 Words 42-year-old man presents to ED with 2-day history .docx
800 Words 42-year-old man presents to ED with 2-day history .docx800 Words 42-year-old man presents to ED with 2-day history .docx
800 Words 42-year-old man presents to ED with 2-day history .docxpriestmanmable
 
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docxpriestmanmable
 
8.0 RESEARCH METHODS These guidelines address postgr.docx
8.0  RESEARCH METHODS  These guidelines address postgr.docx8.0  RESEARCH METHODS  These guidelines address postgr.docx
8.0 RESEARCH METHODS These guidelines address postgr.docxpriestmanmable
 
95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docx95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docxpriestmanmable
 
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docxpriestmanmable
 
8-10 slide Powerpoint The example company is Tesla.Instructions.docx
8-10 slide Powerpoint The example company is Tesla.Instructions.docx8-10 slide Powerpoint The example company is Tesla.Instructions.docx
8-10 slide Powerpoint The example company is Tesla.Instructions.docxpriestmanmable
 
8Network Security April 2020FEATUREAre your IT staf.docx
8Network Security  April 2020FEATUREAre your IT staf.docx8Network Security  April 2020FEATUREAre your IT staf.docx
8Network Security April 2020FEATUREAre your IT staf.docxpriestmanmable
 

Mehr von priestmanmable (20)

9©iStockphotoThinkstockPlanning for Material and Reso.docx
9©iStockphotoThinkstockPlanning for Material and Reso.docx9©iStockphotoThinkstockPlanning for Material and Reso.docx
9©iStockphotoThinkstockPlanning for Material and Reso.docx
 
a 12 page paper on how individuals of color would be a more dominant.docx
a 12 page paper on how individuals of color would be a more dominant.docxa 12 page paper on how individuals of color would be a more dominant.docx
a 12 page paper on how individuals of color would be a more dominant.docx
 
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx
978-1-5386-6589-318$31.00 ©2018 IEEE COSO Framework for .docx
 
92 Academic Journal Article Critique  Help with Journal Ar.docx
92 Academic Journal Article Critique  Help with Journal Ar.docx92 Academic Journal Article Critique  Help with Journal Ar.docx
92 Academic Journal Article Critique  Help with Journal Ar.docx
 
A ) Society perspective90 year old female, Mrs. Ruth, from h.docx
A ) Society perspective90 year old female, Mrs. Ruth, from h.docxA ) Society perspective90 year old female, Mrs. Ruth, from h.docx
A ) Society perspective90 year old female, Mrs. Ruth, from h.docx
 
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx
9 dissuasion question Bartol, C. R., & Bartol, A. M. (2017)..docx
 
9 AssignmentAssignment Typologies of Sexual AssaultsT.docx
9 AssignmentAssignment Typologies of Sexual AssaultsT.docx9 AssignmentAssignment Typologies of Sexual AssaultsT.docx
9 AssignmentAssignment Typologies of Sexual AssaultsT.docx
 
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx
9 0 0 0 09 7 8 0 1 3 4 4 7 7 4 0 4ISBN-13 978-0-13-44.docx
 
900 BritishJournalofNursing,2013,Vol22,No15©2.docx
900 BritishJournalofNursing,2013,Vol22,No15©2.docx900 BritishJournalofNursing,2013,Vol22,No15©2.docx
900 BritishJournalofNursing,2013,Vol22,No15©2.docx
 
9 Augustine Confessions (selections) Augustine of Hi.docx
9 Augustine Confessions (selections) Augustine of Hi.docx9 Augustine Confessions (selections) Augustine of Hi.docx
9 Augustine Confessions (selections) Augustine of Hi.docx
 
8.3 Intercultural CommunicationLearning Objectives1. Define in.docx
8.3 Intercultural CommunicationLearning Objectives1. Define in.docx8.3 Intercultural CommunicationLearning Objectives1. Define in.docx
8.3 Intercultural CommunicationLearning Objectives1. Define in.docx
 
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx
8413 906 AMLife in a Toxic Country - NYTimes.comPage 1 .docx
 
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx
8. A 2 x 2 Experimental Design - Quality and Economy (x1 and x2.docx
 
800 Words 42-year-old man presents to ED with 2-day history .docx
800 Words 42-year-old man presents to ED with 2-day history .docx800 Words 42-year-old man presents to ED with 2-day history .docx
800 Words 42-year-old man presents to ED with 2-day history .docx
 
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx
8.1 What Is Corporate StrategyLO 8-1Define corporate strategy.docx
 
8.0 RESEARCH METHODS These guidelines address postgr.docx
8.0  RESEARCH METHODS  These guidelines address postgr.docx8.0  RESEARCH METHODS  These guidelines address postgr.docx
8.0 RESEARCH METHODS These guidelines address postgr.docx
 
95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docx95People of AppalachianHeritageChapter 5KATHLEEN.docx
95People of AppalachianHeritageChapter 5KATHLEEN.docx
 
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx
9 781292 041452ISBN 978-1-29204-145-2Forensic Science.docx
 
8-10 slide Powerpoint The example company is Tesla.Instructions.docx
8-10 slide Powerpoint The example company is Tesla.Instructions.docx8-10 slide Powerpoint The example company is Tesla.Instructions.docx
8-10 slide Powerpoint The example company is Tesla.Instructions.docx
 
8Network Security April 2020FEATUREAre your IT staf.docx
8Network Security  April 2020FEATUREAre your IT staf.docx8Network Security  April 2020FEATUREAre your IT staf.docx
8Network Security April 2020FEATUREAre your IT staf.docx
 

Kürzlich hochgeladen

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Kürzlich hochgeladen (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx

  • 1. ISTA 130: Lab 2 1 Turtle Review Here are all of the turtle functions we have utilized so far in this course: turtle.forward(distance) – Moves the turtle forward in the direction it is currently facing the distance entered turtle.backward(distance) – Same as forward but it moves in the opposite direction the turtle is facing turtle.right(degrees) – Roates the turtle to the right by the degrees enteres turtle.left(degrees) – Same as right, but it rotates the turtle to the left turtle.pensize(size) – Adjusts the size of the line left by the turtle to whatever value is entered for size turtle.home() – Moves the turtle to the default location and faces it to the right turtle.clear() – Clears all the lines that were left by the turtle in the window. turtle.penup() – Causes the turtle to stop leaving lines (until pen is placed back down)
  • 2. turtle.pendown() – Places the pen back down to the turtle can continue leaving lines when forward and backward are called. turtle.pencolor(color string) – Changes the color of the lines left by the turtle to whatever color string entered (so long as Python recognizes it). turtle.bgcolor(color string) – Changes the background color for the window that the turtle draws in. turtle.speed(new speed) – Changes the speed at which the turtle moves to whatever newSpeed is. turtle.clearscreen() – Deletes all drawings and turtles from the screen, leaving it in its initial state Note that abbreviations also exist for many of these functions; for example: � turtle.fd(distance) � turtle.rt(degrees) � turtle.pu() 1 2 Functions and Parameters Here is the square function we looked at yesterday: def square(side_length): ’’’
  • 3. Draws a square given a numerical side_length ’’’ turtle.forward(side_length) turtle.right(90) turtle.forward(side_length) turtle.right(90) turtle.forward(side_length) turtle.right(90) turtle.forward(side_length) turtle.right(90) return square(50) # This would give side_length the value of 50 square(100) # This would give side_length the value of 100 print side_length # This will give an error because side_length # only exists inside the function! Try it out: (1 pt.) Create a new file called lab02.py. In this file, create a simple function called rhombus. It will take one parameter, side length. Using this parameter, have your function create a rhombus using turtle graphics. Call your rhombus function in the script. What happens if you provide no arguments to the function? Two or three arguments? Then, modify your rhombus function so it takes another argument for the angle inside the rhombus. 3 Data types Python recognizes many different types of values when working with data. These can be numbers,
  • 4. strings of characters, or even user defined objects. For the time being, however, were only going to focus on three of the data types: integer – These are whole numbers, both positive and negative. Examples are 5000, 0, and -25 float – These are numbers that are followed by a decimal point. Examples are 26.58, 0.0, and -1.23 string – These are a string of characters that include letters, punctuation, symbols, and “white- space” characters. They are always surrounded by double quotes (”) or single quotes (’). Examples are “Hello World!”, “[email protected]#%#!” and “I like the number 47”. 2 It is important to know what type of values you are going to be working with when writing code in Python. Let’s take our square example from above. We know what happens if we call the square function using an integer, but what happens if we call it with a float or string? Try it out: (1 pt.) Copy this code for the square function above into your lab02.py. In the main function, try calling this function and passing in an integer. Next, try to pass in a float (just replace the integer value with a float, no need to call the function twice). Finally, try calling it by passing it a
  • 5. string value. Now that youve seen the results of passing it a string, what happens if you pass the square function the string “100”? 3.1 Operators and data types In class we learned some useful operators: + addition - subtraction * multiplication ** power/exponent function / division // integer division How do these operators apply to different data types? Try it out: In your lab02.py, try each of these 6 operators on every combination of int, float, and string, and write down in a comment whether the result is an integer, a float, a string, or an error. Also, take note if anything unexpected happens. There should be six combinations for each operator (int & int, int & float, int & string, float & float, float & string, string & string), so you should have 36 lines of code at the end of this! Do you see any patterns? Is there anything unexpected? (2 pt.) 3.2 Converting between data types Copy this program into a new, temporary script, and try it out. What do you expect it to do?
  • 6. What does it do? print "Enter first number: " num1 = raw_input() print "Enter second number: " num2 = raw_input() numsum = num1+num2 print "The sum is:" + numsum 3 Now try this program. What do you expect it to do? What does it do? print "Enter a number: " num1 = raw_input() doublenum = num1 / 2 print "Half that number is:" + doublenum And finally, try this program. What do you expect it to do? What does it do? print "This one should be easy." four = 2 + 2 print "2 + 2 = " + four The problem: all three of these programs are mixing data types! Specifically, they’re trying to combine strings and integers. When a user enters something for raw input, the result is a string, not a number. Fortunately, Python is able to convert between data types, using these three functions:
  • 7. int() – convert the argument to an integer str() – convert the argument to a string float() – convert the argument to a float (decimal) number Try this example code to get an idea of how these functions work: x = 5 y = ’13’ print x + int(y) print str(x) + y Try it out: (3 pt.) Fix all three of the short programs above so they have the desired behavior, and copy them into your lab02.py. 4 For loops Functions are useful if we know we want to run some code a small amount of times. What happens if we want to run it thirty times? A hundred times? If that happens, it would be a huge hassle and waste of time to have to type out the function call thirty or a hundred times. We can use for loops to easily run code a specific number of times. Example: for i in range(<number of times to repeat>): <code goes here>
  • 8. 4 Using these loops, we only have to change the number in the range() function to alter the number of times the code gets repeated, instead of having to type out or delete the code each time. For instance, if we wanted to create a lot of triangles next to each other, we could create a triangle function to use in a for loop. Example: for i in range(30): triangle(50) turtle.forward(50) What happens if you change the 30 in the range function to other values positive integer values? What about 0 or negative values? Floats? Try it out: (3 pt.) Create a “polygon” function that draws a polygon of any size, with any number of sides. For example, the command “polygon(5, 11)” should draw a hendecagon with sides of length 5. You may need to remember a bit of geometry for this; try making a triangle function, a square function, a pentagon function, and see if you find a pattern. You’ll also need to use a for loop in your code, since we don’t know how many sides the polygon will have until we run the code.
  • 9. Then, modify your script so that it asks the user for a size and number of sides, then draws the requested polygon. 5 What to turn in When you’re finished, submit your lab02.py file to the Lab 2 dropbox on D2L. Total Points: 10 points possible 5 Turtle ReviewFunctions and ParametersData typesOperators and data typesConverting between data typesFor loopsWhat to turn in ISTA 130: Homework 2 Due June 5 before class 1 Debugging Today’s homework deals with debugging. This is the process that occurs when you find out your code doesn’t work, and you try to figure out why. Download the file errors.py from D2L. This file contains a script with a number of errors in it. Some are programming language errors, which Python will report with error messages. Others are logic errors – code that runs, but does not do what the programmer intended. Your task is threefold:
  • 10. 1. Determine which lines have an error 2. In a comment after the line, write down the type of error message Python gives (if any), and a brief description of why this line has a problem 3. Correct the error so that the program has the intended behavior For example, if the line of code is: 99bottles = 5 Python will give the error: File "<stdin>", line 1 99bottles = 5 ^ SyntaxError: invalid syntax so the corrected line in your code should look like: ninetyninebottles = 5 # SyntaxError: variable names can’t start with a number If there are multiple errors of the same type in a row, you don’t have to put the same comment for all of them; just comment the first and fix the rest. The code has 15 problems by my count; each will be worth 1 point (15 pt.). Any additional improvements to the code may be worth extra points (2 pt.). 2 What to turn in
  • 11. Submit your corrected errors.py script to the Homework 2 Dropbox folder in D2L: Total Points: 15 points possible Extra Credit: 2 points possible 1