Lesson 16



AOIT Introduction to ProgrammingLesson 2Writing a Simple ProgramStudent ResourcesResourceDescription Student Resource 2.1Worksheet: Running Python in Interactive Mode Student Resource 2.2Worksheet: Circle ProgramStudent Resource 2.3Guide: Basic Python Debugging TechniquesStudent Resource 2.4Worksheet: Target ProgramStudent Resource 2.5Worksheet: Drawing and SequencingStudent Resource 2.6Worksheet: Olympic Rings ProgramStudent Resource 2.7Worksheet: ReflectionsStudent Resource 2.1Worksheet: Running Python in Interactive ModeStudent Names:_______________________________________________________ Date:___________Directions: This worksheet contains exercises for you to do using the Python shell (IDLE). The exercises cover integer and floating-point arithmetic, character (that is, text) manipulation, and variables. Do the problems in each section below and then record your observations and analyses by answering the associated questions.Integer ArithmeticType the following problems into IDLE after the shell prompt (>>>). Then press Enter so that Python runs the calculation and returns the result. When you are finished running the problems, answer the questions. Note that the plus (+) and minus (–) symbols are used for addition and subtraction, as you might expect. Some of the other symbols might be new to you and worth noting.Problems:5 + 65 – 22 * 56 / 32 ** 49 / 2 Questions:What symbol is used for division in Python?What symbol is used for multiplication?What symbol is used for exponentiation? What is exponentiation?Floating-Point ArithmeticType the following problems into IDLE and answer the questions below.Problems:9.0 / 2.04..65.0 / 0Questions:Explain the results of problems 2 and 3.Analyze and explain the results of problem 4.Large NumbersType the following problems into IDLE and answer the questions.Problems:2345 * 67850 ** 100Questions:Analyze and explain the results of problem 2.CharactersProblems:'a' + 'b''a' * 4'aa' / 2print('Hello' + 'World' + '!')pront("Hi")print("Yes " * 100)Questions:What is the result of problem 1? Do you know the term used when text is combined in this way?What is the result of problem 2? Do you think this operation could be useful in Python programming? Why or why not?What is the result of problem 3? Can you use “/” with characters?What kind of error does Python return when you do problem 5?VariablesA variable is a symbol that substitutes for an actual value (like using x in algebra for an unknown number). Type the following statements into Python (be sure to press Enter after each one). Then answer the questions.Python statements:x = 3xx * xQuestions:What result did you get back from Python after typing in the first statement? Explain the result. Hint: This is an assignment statement that sets the value of the variable x.What result did you get back from Python after typing in the second statement? Explain the result. What result did you get back from Python after typing in the third statement? Explain the result.Student Resource 2.2Worksheet: Circle ProgramStudent Names:_______________________________________________________ Date:___________Directions: Use this worksheet to plan the changes you will make to the Circle program. You will be changing the color and size of the circle, as well as the location of the circle in the drawing window.Here is the code for the Circle program. Note that the # character signals comments that help you understand the code. Also note that some programming statements require a turtle. prefix and some do not. (The ones that communicate directly with the Turtle Graphics drawing routines need the prefix.)## circle.py# Draws a circle using Python Turtle Graphics# Tested with Python 3.2.2 on Windows XP and Windows 7## Fri Dec 9, 2011# Copyright 2009-2012 National Academy Foundation. All rights reserved.## # set up the program## import turtle drawing routinesfrom turtle import *# create the drawing windowsetup()# create a title for the drawing windowtitle("circle program")# create a turtle to draw withturtle = Turtle()## draw the circle## lift the pen up (away from the window)# so it doesn't draw a line as it movesturtle.up()# set the starting point for the drawing penturtle.goto(0,20)# put the pen down (against the window) # so it can begin to drawturtle.down()# set the pen colorturtle.color("red")# set the pen widthturtle.width(2)# call turtle.begin_fill() before drawing the circleturtle.begin_fill()# draw the circleturtle.circle(50)# call turtle.end_fill() after drawing the circleturtle.end_fill()# # hide the turtle#turtle.hideturtle()## end the program#done()Before you make any changes to the Circle sample program, save your own copy of it in your Lesson 2 folder. It is your copy of the program that you will be changing.To change the color of the circle:Write the statement below that you will modify in order to change the color.Now write your modified Python statement to change the color to a color of your choice.Change the statement in the program, save your program, and run it. Did you get the expected result? If not, what did you get?To change the size of the circle:Write the statement that you will modify in order to change the size of the circle.Write your modified Python statement to change the size to a size of your choice.Change the statement in the program, save your program, and run it. Did you get the expected result? If not, what did you get?To change the location of the circle:Write the statement that you will modify in order to change the location of the circle.Write your modified Python statement to change the location to a location of your choice.Change the statement in the program, save your program, and run it. Did you get the expected result? If not, what did you get? Student Resource 2.3Guide: Basic Python Debugging TechniquesSyntax ErrorsSyntax deals with the form and structure of statements in a programming language. A program containing a syntactic, or syntax, error fails execution. Syntax errors in Python statements are detected by the Python parser, which stops execution and displays a pop-up error message. Here is an example of an error (the word from is misspelled) and the resulting error message:Python flags a syntax error when it can’t parse part of a program. When parsing fails, Python flags in red where the parsing error occurred. The place it flags may be at the exact spot in the program where the error appears, or it may be before or after that spot, so you may have to look around a little to figure out what is actually wrong.Notice that Python flags the string “turtle” in the above example, rather than “frum,” which is the actual error that needs to be fixed.Runtime ErrorsErrors in Turtle Graphics functions (for example, setup() or begin_fill()) are not detected until runtime. Here is such an error (the word title is misspelled), as well as the resulting error message in the Python shell:# create a title for the drawing windowtitel("circle program")Semantic ErrorsA program containing a semantic error completes execution, but it does not produce the results that were intended. Of course, semantic errors are more difficult to debug and fix. Programmers mostly rely on looking at the results (often at the observable changes between one program run and another) and on intuition and experience to find and fix errors.For example, knowing how large the default drawing window is, what do you suppose would happen if you wrote a circle program where the circle radius is 500 instead of 50 (for example, turtle.circle(500)). Try it and find out! Student Resource 2.4Worksheet: Target ProgramStudent Names:_______________________________________________________ Date:___________Directions: In this assignment, you write the code to draw a set of three concentric circles, each of a different color, that form a target. Before you begin, read through this worksheet to make sure you understand the assignment and the assessment criteria. Next, plan the coordinates and sequence of your target program using this worksheet. Then create a Target program and save it in your Lesson 2 folder. The target that displays in the drawing window when you run your program should look something like this:Planning StageUse a piece of graph paper to plan the coordinates of your three circles.Plan the order of the statements for your program here.What statements do you need to start the drawing program?Which circle will you code first, and what statements will you use? Tip: Draw the largest circle first. The two smaller circles must be drawn on top of the large circle. Tip: The turtle starts tracing the path for the circle at the bottom. It draws in a counterclockwise direction. Which circle will you code second, and what statements will you use?Which circle will you code third, and what statements will you use?What statement do you need to code to end the program?Coding StageTo begin a new program, open the Python shell, select File > New Window, and begin typing. Before you get too far, save your program in your Lesson 2 folder as target.py. Save often as you work. Remember to include comments before each line of code to explain what the statements are doing. (And remember to start each comment with #.)Run the program to check that it creates the target you planned.Make sure the program ends normally when you click the x in the upper-right corner of the drawing window.Fix any syntax and semantic errors until your program draws the perfect target.Make sure your assignment meets or exceeds the following assessment criteria:The “Planning Stage” section of the worksheet clearly shows how you will create the target.The circles used to make the target are mapped out correctly on graph paper.The syntax is accurate, and all the necessary statements are included.The program produces a target that displays in the window with three circles of different colors.The comments summarize the code. Student Resource 2.5Worksheet: Drawing and SequencingStudent Names:_______________________________________________________ Date:___________Directions: Use this worksheet to draw the three Python windows you have been working in so far and to put the programming steps from the previous section in the right order.Draw the Python shell window here. (The Python shell window is the one that opens when you double-click the IDLE Python GUI icon.) Put in as much detail as you can (for example, the location of the File menu).Draw the Python editor window here. (This is the window you have used to create your Python programs.) Put in three to four lines of either the Circle program or the Target program. Label the menus you have used so far (for example, the Run menu).Draw the Turtle Graphics drawing window here. (This is the window where Turtle Graphics draws the circles.) Draw in the circle from the Circle program or the target from the Target program.The following tasks are actions you took as you wrote the Target program, but they are not listed in the order you actually did them. Number them from 1 to 10 in the correct order (1 is the first task you did, and 10 is the last).Type in the statements to draw the largest circle. Ahead of each statement, type in a comment explaining what it is doing.Save the program.Type in the done() statement. Ahead of the statement, type in a comment.Type in the statements to draw the middle circle. Ahead of each statement, type in a comment.Open the Python shell.Type in the setup() statement. Ahead of the statement, type in a comment.In the Python shell, select File > New Window.Type in the statement from turtle import *. Ahead of the statement, type in a comment.In the Python editor, select Run > Run Module.Type in the statements to draw the smallest circle. Ahead of each statement, type in a comment.Student Resource 2.6Worksheet: Olympic Rings ProgramStudent Names:_______________________________________________________ Date:___________Directions: In this assignment, you write the code to draw five intersecting rings that resemble the Olympic rings. Before you begin, read this worksheet to make sure you understand the assignment and the assessment criteria listed at the end. Next, use this worksheet to plan the coordinates and sequence of your Olympic Rings program. Then create an Olympic Rings program and save it in your Lesson 2 folder. The Olympic rings you create should look something like this (there are five rings here—the yellow one might not show up very well):Planning StageUse a piece of graph paper to plan the coordinates of your five circles.Plan the order of the statements for your program here.What do you need to do to start the drawing program?Which circle will you code first, and what statements will you use? Tip: You could do the rings in any order, but it might be easier if you first do the three top rings and then do the two bottom ones. Do you see why that is true?Which circle will you code second, and what statements will you use?Which circle will you code third, and what statements will you use?Which circle will you code fourth, and what statements will you use?Which circle will you code fifth, and what statements will you use?What statement ends the program?Coding StageTo begin a new program, open the Python shell, select File > New Window, and begin typing.Before you get too far, save your program in your Lesson 2 folder as olympic_rings.py. Save often as you work.Remember to include comments before each line of code to explain what the statements are doing.Run the program to check that it creates the Olympic rings you planned.Make sure the program ends normally when you click the x in the upper-right corner of the drawing window.Fix any syntax and semantic errors until your program draws perfect Olympic rings.Make sure your assignment meets or exceeds the following assessment criteria:The “Planning Stage” section of the worksheet clearly shows how you will create the Olympic rings.The circles used to make the rings are mapped out correctly on graph paper.The syntax is accurate, and all the necessary statements are included.The program produces the five intersecting circles of different colors.The comments summarize the code.Student Resource 2.7Worksheet: ReflectionsStudent Name:_______________________________________________________ Date:___________Directions: Congratulations on creating your first Python programs! Use this sheet to reflect on the reality of your first programming experiences compared to what you expected before you started this course. Write short answers to the following questions in the space provided.At the beginning of this lesson, you speculated as a class about what Python programming projects would be like. Were the projects you just completed different from what you expected?Did you make it through any of the programming projects without encountering any bugs? If not, what were some of the bugs in your programs? As you can tell, programming requires a lot of precision and persistence in order to achieve a valuable product. How do you see yourself developing these work habits to qualify yourself for a career in programming? How did you and your programming partner function as a team? Was working as a team an advantage? Were there any disadvantages to working with a partner?Based on what you’ve learned already about programming in Python, what project(s) do you think would be fun to do next? Explain why you think you’d like to do them. Do you think the projects you’d like to do would help you learn what you’d need to know to be a professional programmer? Why or why not? ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download