ECS 102 - Baruch Lab 5 Spring 2019 Name NetID (login, like email, not your SUID) Today you will be doing some more experiments in the shell. Create a file Lab5.txt. In this file you will be asked to save parts of the shell (just copy and paste). Don't copy all your experiments, just the final good ones. I. Exploring built in Functions. You may use IDLE or Thonny today. Write your name and "Part I" in the file Lab5.txt There are some functions built into Python itself. Try these: A. >>> x=4.7 >>>y=2 >>>print(type(x)) >>>print(type(y)) B. >>>a = int(x) >>>print("a=",a,"\tx=",x) The \t represents a tab character. >>>b = float(y) >>>print("b=",b,"\ny=",y) The \n represents a new line character. Were the values stored in x and y changed? (You don't need to debug, you can look at the output.) C. >>>r=8.9 >>>round(r) >>>s=6.5 >>>round(s) 1
>>>t=1.4999 >>>round(t) >>> print("r=",r,"\ts=",s, "\tt=",t) Were the values stored in r, s, and t changed? Copy the shell for part I to Lab5.txt. (Save!) II. Using math functions. Restart the shell. Write "Part II" in the file Lab5.txt In the class notes for today is a list of built in math functions. You will need to import math to use them. >>> import math A. Try >>> sin(90) What happens? The problem is that to use the math functions, we need to put math. in front of them. Try >>> math.sin(90) >>> math.cos(90) Does it look like Python is using degrees for sin and cos? Try >>>math.pi Result to 5 decimal places: >>>math.sin(math.pi/2) >>>math.cos(math.pi/2) Does it look like Python is using radians for sin and cos? 2
B. Write a few lines of code that will print a table of sines and cosines, from 0 degrees to 180 degrees (inclusive) in steps of 10 degrees. You will need to use the formula onedegreeinradians= math.pi/180, so to compute sin(10 degrees) you will need to compute sin(10*onedegreeinradians). Make sure your table has the right number of rows. Don't worry if the columns don't line up nicely. Just make sure there are three columns, for degrees, sin, and cos. Looking at the output. Does the table give exactly 1 for cos(0)? exactly 0 for sin(0)? Does the table give exactly 0 for cos(90 degrees)? exactly 1 for sin(90 degree)? Does the table give exactly -1 for cos(180 degrees)? exactly 0 for sin(180 degrees)? Make a guess about why you got some "no"s for you answers above and write it here. Copy the shell for your good solution of II B with the output table, to part II of Lab5.txt. (Save!) Print Lab5.txt III. More loops, accumulating a product. A. In addition to accumulating a sum as we did in Lab 4, sometimes we want to accumulate a product. For example 7! = 1 * 2 * 3 * 4 * 5 * 6 * 7. We can compute this in a loop, by each time multiplying the product by one more number. To start out our process, the product has to be initialized to 1, not zero. Why? Write a program, in a module file Lab5PartIII.py, with a main() and a call to main(), to compute 10! Save the program. Run the program. What does it get for 10!?: B. ints and floats Modify the program to compute 10! and 10.0! using ints (1*2*3...) and floats(1.0*2.0*3.0...) Print both results. Are the int and float results the same? Save your program. 3
C. 100! Modify your program to ask the user for an int value k. It should then compute k!, both the int and the float versions. (Use float for each term in the computation, not just for the final result.) Run the program, entering the value 10, and check that you get the same results as for part B. Now run the program and enter 100, for 100!. Are the int and float results exactly the same as each other? Are they close? Save your program. Save a copy as Lab5partIII.txt and copy your good output (that is, the computed values of 10! and 100!) to the same textfile, at the bottom, labeled "output". Make sure your name is at the top of the file. Print the text file, and close Lab5partIII.py. IV. Introduction to graphics. A. To do graphics you will need to download the graphics.py file and put it in your Python Programs folder. (This is where you should be saving all you programs.) This is the link, http://mcsp.wartburg.edu/zelle/python/ but it is also on the lecture page. B. While you are at that site you should download the Graphics Resource pdf file, too. Put it in a good place on your H: drive - you will want to refer to it often. C. From our course lecture webpage, download the file FourCorners.py. From the shell open that file. Put your name and section at the top, where indicated in the comment. Save the file. We will be adding to this during lab. D. Run the program. You should see a window with a circle, like this here: Click the mouse in the window to close the window. i) Look at the code creating the window. How many pixels wide is the window? How many pixels tall is the window? 4
ii) On this picture, write coordinates for the four corners of the windows. For example, label the upper left corner (0,0). (Make use of your answer to part i) iii) Give the coordinates for the center of the green circle. (Look at the code for the circle.) Write the coordinates on the picture above, on the green circle. iv) I want to draw 3 more circles, in the other 4 corners, a bit in from the corners just as the green circle is. On the paper picture, label where the centers should be, by writing the coordinates at those points. v) Create and draw the other 3 circles. vi) Color the upper left circle yellow, the lower right circle red, and the lower left circle blue. Run the program and make sure the window with the picture stays open till you click the mouse in the window. Print FourCorners.py by making a.txt file and printing that. Take a screen shot of your graphics window, Four Corners, with all 4 circles. V. Submitting your work. Make sure your name is on this lab packet. Staple Lab5.txt Lab5partIII.txt and Four Corners.txt to the Lab 5 packet. Turn the lab in at the file cabinet. Upload the screenshot of the graphics window Four Corners using the link on today's lecture page. 5