Lab Assignment #1 - Getting Started



Lab 1 September 2, 201500CS-1004, Introduction to Programming for Non-majors, A-term 201500CS-1004, Introduction to Programming for Non-majors, A-term 2015 Lab 1 — Functions and Program StructureDue: at 11:59 pm on the day of your lab sessionObjectivesTo write a simple function with parameters To write a definite for-loop To learn how to use the Turnin systemNote: don’t worry if you can’t finish the entire lab exercise. Use Turnin to submit as much as you’ve completed before you leave the lab. Make sure you finish the rest of the lab on your own time.Before you startSign the attendance sheet.Function with parametersStudy the following figure:–Given the length S > 0 of the side of the square and the value a (with 0 < a S), you are to compute the inner area c × e. The formulas for the various values b, c, d, and e are shown in the figure under “Known Facts.”To solve this problem, create a Python function named innerArea with two parameters, S and a, that computes and prints the area of the inner rectangle.Structure of your ProgramYour program should be written in a Python module called “lab1.py.” To create it, start IDLE and open a new window by selecting File > New File. Save this file immediately as lab1.py. Type your program into this file. It should look like:–# Lab 1# Author: <<Your Name>>def innerArea(S, a):# computation# computation# …print ("Output information")returnOnce you have completed this innerArea function, run the lab1.py module by invoking the Run > Run Module command from the IDLE menu. Then, in the main IDLE window, type the command innerArea(1,0.5).The output should look like the following :–>>> innerArea(1, 0.5)S=1 a=0.5 Area=0.46875The printed output from innerArea() must appear on a single line by itself. Repeat the innerArea command for different values of S and a.If you have to modify the innerArea function, be sure to invoke Run>RunModule again. Do not continue until you have a working innerArea() function that computes the above value properly. Note: you may ignore a rounding error in the precision of your pleting Homework #1You should now have enough Python expertise to complete all of the questions of Homework #1. In Homework #1, you don’t need parameters for your functions, but you will need them in subsequent Homework assignments.Definite for-loopThe next part of the lab uses a structure that we have not yet covered in class, namely the Definite for-loop. This is introduced on Page 15 of the textbook. Study this before you come to the lab session.A definite for-loop deals with the repetition of a fragment of code a fixed number of times. Sometimes you want to perform a fixed number of iterations of a specific task. For example, what if you were asked to print your name on five consecutive lines? You might respond by the following program. In fact, add this function declaration to the end of your?lab1.py?program.def bigSignature():print ("Your name here")print ("Your name here")print ("Your name here")print ("Your name here")print ("Your name here")returnNote:Be sure to leave a blank line after your?innerArea function so it remains a distinct function from bigSignature.Run this function to verify that it works. How do you do this? As before, invoke the Run > Run Module command in the IDLE window of the module lab1.py. After you see the?===== RESTART ======?line in the Python shell window, type?bigSignature()?at the?>>>?prompt.While this works, it sure doesn’t feel right! What if you had to print your name ten or a hundred times? Instead, you need a way to repeat a body of statements a fixed number of times.Replace the above code with the following and demonstrate to yourself that it runs and produces the same output.def bigSignature(): for index in range(5): print ("Your name here") returnNote that you must properly indent this program, once for the function definition, and once more for the?for-statement.Basically, a colon followed by a sequence of indented lines works the same way in for-loops as it does in function definitions. Each indented line is effectively a continuation of the line ending in the colon. Taken together, the indented lines make up the code fragment that the line with the colon wants to execute.First, run this function to verify that the output is the same. Now look at the code and try to “read it”. For example, change the “5” to a “3” and see what happens.This statement is known as the?definite for-loop. You will need this in Homework #2.Definite for-loop with a body of statementsTo get a better understanding of the concept, modify the BigSignature function as follows:–def bigSignature(): for index in range(5): print("Hi!") print ("Your name")# substitute your name here returnSave this revised module and run it. The terminal will RESTART, and when you invoke the bigSignature() function you will see that these two statements are executed “as a body” that is repeated five times. Thus the total output consists of ten lines of output, alternating between “Hi” and “Your name” (or whatever you substituted).Purpose of the index variableThe for-statement requires a variable that is used to iterate over the total number of requested iterations. The examples above defined a variable named index for this purpose, but it can be any name that you choose.The first time the body is executed, the value of index is 0; the second time its value is 1; the third time its value is 2; the fourth time its value is 3; the fifth and final time its value is 4. You should see a pattern here.Demonstrate this by modifying the function as follows:def bigSignature(): for index in range(5): print ("Pass", index) print ("Your name")Reload the module and invoke bigSignature() one more time. As you can see, the body of statements is executed as before, but this time the value of index is printed with each iteration through the loop.SubmissionSave your lab1.py file, and then visit the Turnin system atHYPERLINK "" into Turnin using the password that you set in response to its welcome message. Then submit lab1.py under the assignment Lab 1. After submission, view your Turnin account to see that it has received your submission. If you are working with someone in the lab due to limited space, put BOTH names on your lab1.py file; each person should submit an identical copy.If this lab (or any lab) is too easy, please complete it as quickly as possible, and then spend your time helping a classmate for whom it is not so easy. Also, please inform the TAs that you have done so in order to earn extra Lab credit.Advanced ungraded questionDo not attempt this question until after you have submitted your lab1.py file.Given what you have learned in this lab, can you write a function areaTable() the produces the following output using the innerArea() function that you wrote earlier in this lab?>>> areaTable()S=2 a=1 Area=1.875S=3 a=2 Area=5.18518518519S=4 a=3 Area=10.359375S=5 a=4 Area=17.472S=6 a=5 Area=26.5509259259S=7 a=6 Area=37.6093294461S=8 a=7 Area=50.654296875S=9 a=8 Area=65.6899862826S=10 a=9 Area=82.719Note that the precision of your calculations may be slightly different from these. ................
................

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

Google Online Preview   Download