ECS 102 - Baruch Lab 2 Fall 2019 Name I. Review Recall that in lab 1, you created a folder on your H: drive, Document/Apps-SU/Python Programs The reason you need to save programs here on the lab computers is a) So they won t be erased when you log off. b) So they will run. (On your own laptop, you can put them anywhere you want.) Answer these by remembering what happened in lab 1. 1. If I enter at the shell prompt >>> 34 what will the shell print? 2. If I enter at the shell prompt>>> 2 + 10 # + 100 what will the shell print? Why? II. A. Variables Open IDLE for Python 3.7 (NOT 2.7!) You see the Python Shell. (From the title bar at the top you can see that is says Python 3.7.0 Shell ) At the prompt (>>>) type # your name (use your actual name!) At the prompt set >>> area=3*4 Evaluate by asking >>> area Create two more variables by assigning length the value 7 and width the value 5. Have the shell evaluate length and width and see that their values are 7 and 5. At the prompt set >>> area=length * width Evaluate. What did you get? Notice that the former value of area (12) has been replaced with a new value.
Give length a new value, 10 Without telling the shell to recompute area, have the shell evaluate the area. What value did it give for area? Did it use the new value of length? Once again tell it to compute area using area=length * width What value did it give for area? Did it use the new value of length? (Changing a value of a variable does not change previous computations, only those going forward.) B. Restarting the Shell From the Shell menu select Restart Shell. Notice that what you typed before is still visible in the window, but there is a line indicating where the shell was restarted. So what does restart mean? At the prompt type area Did it remember the value? (Everything that was defined in the earlier incarnation of the shell has been wiped out.) III. Some data types and commands A. Enter each of these at the prompts, as indicated. Record what the computer responds. Include single or double quotes if they are part of the output. >>> 27 >>> "27" >>> apples = 6 >>> apples >>> "apples" >>> oranges = "navel" >>> oranges >>> 3.14159 >>>type(apples)
>>>type(oranges) >>>type(3.14) >>>type("something in quotes") A string is a sequence of characters. We tell the computer we mean a string by enclosing the string in single quotes ' ' or double quotes: " ". The computer indicates a string by enclosing them in quotes. Above, a sequence of characters without quotes, like oranges, is the name of a variable. A variable could be holding an int, like 27, a float, like 3.14159, or even a string, like "navel". B. We can tell the computer to print something to the screen. Try >>>print(234) >>>print("pizza and salad") >>>print(apples) >>>print(oranges) >>>print(123 + 7) >>>print("i like " + oranges) >>>print(apples + oranges) (What happens?) Answer by looking at the examples above: What happens when you add two strings? What happens when you add a string to a number? >>>print(3, 2, 1) >>>print(apples, oranges, "plums") >>>print(apples,oranges,"plums") >>>print(apples, oranges, "plums") C. Some input. Try the following >>>name = input("enter your name: ") When the computer asks you to enter you name, type it in, followed by the enter key. Now evaluate name >>>name
Write similar code to ask for someone's age. Enter an age when asked. Evaluate age. Are there any quotation marks around the output? Is the computer interpreting your age as a number or as a string? Try >>>age2=eval(input("enter your age ")) >>>age2 Are there any quotation marks around the output? This time, is the computer interpreting your age as a number or as a string? D. Time to write your own program. Restart the shell. (Shell -> Restart shell) Write a sequence of statements that will Ask a user for their name and store it in a variable. Ask the user for the length and width of a rectangle. Compute the area of the rectangle Compute the perimeter of the rectangle Print results like this (but with your values): Hi Michelle! Your rectangle has area 50 Your rectangle has perimeter 30 IV. Congratulations! You have written a program - almost. The problem is, if you want to run it again, you will have to type it all in again. So lets do it again, but this time we will save the statements in a file, so we can reuse them. Select File -> New File A new Python editor window will open. Type in some comments like these (but use your name, not mine): # ProcessRectangle.py # by Marjory Baruch # Compute the area and perimeter of a rectangle. # Ask for name. # Ask for length and width. # Compute area and perimeter. # Print results.
The first three lines are the file name, your name, and a brief description of what the program does. Starting with the comment #Ask for name go back and retype the Python statements you typed in part D. Now there are no prompts. Put the statements right after the comments that describe them, for example # Ask for name. name = input("enter your name ") Do this for all the comments, providing Python statements that will actually do what the comments describe. Select File-> Save In the window that opens, navigate to the folder Documents\Apps-SU\Python Programs on your H: drive that you set up in lab 1. Name the file ProcessRectangle.py and click the Save button. With the ProcessRectangle.py file window selected, select Run-> Run Module from the menu. If all went well, the Python shell window restarted and you are asked to enter your name! Answer the questions when asked, pressing enter key each time, and your program should print some answers! If all didn't go well (which is usually the case), fix errors, save again, and run again. You may have to repeat this step several times. To appreciate that this script can be run over and over choose Run->Run Module again and enter different values. When you run this module, you are just giving the shell lots of lines of code at once. You can still enter commands. At the prompt type >>>length and see that you get the value you entered when you ran the module. Print the file ProcessRectangle.py to hand in later. Close the file window. V. Writing and running a program with a main. From the shell menu select File -> New File. (In the future I will just say open a new file.) Type the following in the window (use your name where it says your name) Don't cut and paste.
# miletokm.py # your name # Converts distance in miles to kilometers # Ask for the distance in miles miles = eval( input( "Enter distance in miles: ")) # Convert to kilometers km = miles * 1.609 # Print the result print(miles, "miles equals", km, "kilometers") Save the module file as milestokm.py in your Python Programs folder on your H: drive. (Recall H:Documents/Apps-SU/Python Programs ) Whenever I say to save a file, this is where you should save it. Select Run-> Run Module from the menu. (I will say Run the Module). Fix errors if there are any. When it is running properly, the shell will be selected and You should see the program's prompt. (Enter distance in miles not >>>) Enter a value, like 1 (where you know the answer) and see what the program says. Did you get a good answer? Run the module again. Did the shell restart? Give a different value. Record the input value (miles) and the output value (kilometers) miles: km: At the shell prompt type miles. What is the output? Save the Python shell window (not the module) in Python Programs as lab2shell.py. Frequently throughout lab save it again, by pressing the control key and s together. That way it won't be lost.
B. Writing a main function. We would like to be able to run a program several times without restarting the shell. Click on the module window. (It says milestokm.py on the title bar.) Click File->Save As. Add v2 (for version 2) to the name (miletokmv2.py), and make sure it is being saved in the python programs folder. Modify your program so it looks like the program below. Indentation matters. To indent a bunch of lines: highlight them and from the menu bar select Format -> indent region. # miletokmv2.py # your name # Converts distance in miles to kilometers def main(): # Ask for the distance in miles miles = eval( input( "Enter distance in miles: ")) main() # Convert to kilometers km = miles * 1.609 # Print the result print(miles, "miles equals", km, "kilometers") Run the module milestokmv2.py. Did the shell restart? Enter data when asked. Now to call main. At the shell prompt type main() and press enter >>> main() Does the program run again? Did the shell restart? C. While we can now convert lots of miles by running the program over and over, lets have the program do it. Save the program again as milestokmv3.py.
Modify the program so it looks like # miletokmv3.py # your name # Converts distances in miles to kilometers def main(): for i in range(4): # Ask for the distance in miles miles = eval( input( "Enter distance in miles: ")) main() # Convert to kilometers km = miles * 1.609 # Print the result print(miles, "miles equals", km, "kilometers") Run the module milestokmv3.py Enter data as asked. How many time does the program ask you for miles? List the values you enter and the answers: miles KM Once again, save the shell. VI. Submitting your work. Print the file lab2shell.txt to printer CST3-116. Print (if you didn't earlier) the module ProcessRectangle.py Make sure your name is on the lab packet. Staple them together in the order: packet on top, then ProcessRectangle, then Lab2Shell. Just outside the lab is a file cabinet with a slot on top and BARUCH ECS102 written on the front. Put your lab in the slot.