CS 100 Introduction to Computer Science Lab 1 - Playing with Python Due September 21/23, 11:55 PM First, a word about due dates. If you are in the Tuesday lab section, your assignment is due Monday night at 11:55 PM. If you are in the Thursday lab section, your assignment is due Wednesday night at 11:55 PM. Second, we have 3 TAs for this class. They will be available in the Kendade lab. They will create a list on the whiteboard. If you need help, you should add your name to the list so that they can talk with you in order. Here is when they are available: Xuefei Chen Hilary Ming Turner Wiley Sunday 7-9 PM Monday 7-9 PM Wednesday 7-9 PM Now, the good stuff. In this class, we will be programming in Python, using a special environment called JES (Jython Environment for Students). Connecting to your MHC file space You should first connect to your MHC file space. To do that, please follow the instructions at https://www.mtholyoke.edu/lits/tech/map_metwork_drives_windowspc When entering the Folder, use the path for your personal file storage, which is: \\mhc\yourusename It is very important that you disconnect your network drive before you leave the lab. To do this, right-click on the computer icon and select the disconnect command. After connecting to your web space, create a folder named cs100. You should put all of your work for this class in this folder. Let s put each week s assignments in a separate folder. So, create a folder named Lab1 inside your cs100 folder. You will save the files that you write this week in that folder.!1
Exploring JES To start JES, click on the Windows icon in the bottom-left corner of the screen. Click on All Programs and scroll to the JES folder. Click on the JES folder and you should see the JES program with a green python head for an icon. Click that file to start JES. A splash screen will appear for a while and then you should see Part 1: Using the command area The command area is the black area at the bottom of the window. Here, you can type commands and get immediate answers. For example, you can use it like a calculator. (10 points) Use the command area to answer these questions. Place your answers in a file called Lab1_yourname and submit that file on Moodle. 1. What does 2 / 3 evaluate to? What does 2.0 / 3 evaluate to? Why are these different? 2. What does 2 + 3 evaluate to? What does + mean when working with numbers? What does Hi + there evaluate to? What does + mean for strings? 3. What does 2 * 3 evaluate to? What does * mean when working with numbers? What does Hi * 3 evaluate to? What does * mean for strings? 4. What does 100 * 3 evaluate to? What does 100 * 3 evaluate to? Why are these different?!2
5. Give the value 2015 to the name year, like >>> year = 2015 Do something similar for birthyear, but giving it the value of the year you were born. Now calculate your age (or the age you will be by Dec. 31 2015, actually) by saying >>> year - birthyear Explain why the expression above gives the answer that you get. Try the functions we created in class Go to the lecture page of the course web site (https://www.mtholyoke.edu/~blerner/cs100/ schedule.html) and download the examples from September 15. In JES, open the File menu and select Open Program Navigate to the file you just downloaded and open it. The file is called Lecture02.py. This should display the functions we created in class with a lot of comments (the lines beginning with #) explaining the functions and how to use them. Click the Load Program button. JES now understands all of these functions and you can use them. 1. Run the hello function from the command area by typing hello () (without the quotes). It should display Hello, world! below the line you typed. 2. Now, try the pickandshow function. 3. Next, edit the showmypicture function so that it includes the name of an image file on your computer. Read the comment above showmypicture to learn how to find the name of a file. Do you understand why this function behaves differently than pickandshow? 4. Now, call showpicture. Notice that this time, you need to pass the filename as a parameter. Make sure you understand why. (20 points) Part 2: Write your own functions To write your own functions, open the File menu and select New Program. At the top of the file, put a comment (a line beginning with #) that includes your name, identifies the lab and whether you are in the Tuesday or Wednesday lab section, like this: # Barbara Lerner, CS 100 Lab 1, Wednesday lab section Save all the functions that you define here in a file called Lab1.py and turn it in on Moodle. 1. Type in this function that prints the result of squaring a number: def square(x): print x * x Now in the command area at the bottom, call the square function with each of these arguments: 3, 3.3, Hello. Record the responses in the doc file you created earlier. (Remember to click Load Program again.)!3
2. Write a function called fahrenheittocelsius. To convert from Fahrenheit to Celsius, you need to subtract 32 from the Fahrenheit temperature and then multiply the result by 5/9. 3. Write a function called feettoyards. The function should take in some number of feet and tell you the number of yards. Make sure the result uses floating point, not integer representation. (For those of you who only know (the more sensible) metric system, there are 3 feet in 1 yard.) 4. Write a function called sayhello. The function should take a name as a parameter and greet the caller by name. For example, sayhello( Mary Lyon ) might print out What s up, Mary Lyon? sayhello( Tom Brady ) would print What s up, Tom Brady?. You should not need to change your function to get different output, only how you call the function in the command area. (10 points) Part 3: Understanding error messages One thing that makes programming hard, is that computers are very picky about exactly how you say something. They are really not very smart!. You need to remember where all the punctuation goes, and to indent the statements inside a function. Python really cares about this! You also need to remember the names of the functions you can use, type them correctly(!), and what the parameters are. For these questions, you will deliberately introduce mistakes in your program. I want you to record what error message you see. Sometimes the same error message may indicate different problems, but it is still a good idea to get used to the error messages and what they mean. I want you to turn in a record of these observations, but also keep a copy for yourself that you can refer to in future labs. For each of these, make the change I suggest, record the error message. Then fix it before going on to the next item in the list. Add the answers to these to the file you created in the first part of the lab where you explored the command area. First, make a copy of the pickandshow function from Lecture03.py in another file. We will introduce errors into it and we do not want to modify the original version! 1. Erase the : at the end of the def line. Load the program. 2. Indent the lines of the function so that it looks like this: def pickandshow () : myfile = pickafile() mypict = makepicture(myfile) show(mypict) 3. Change mypict to mypic (remove the t) in the call to the show function on line 4. You should see no errors when you load the program. Now call pickandshow() in the command area. What happens? 4. Replace mypict with 100 in the call to show. Load the program and then call pickand- Show(). 5. Remove the parameter mypict when you call show. Load the program and then call pickandshow().!4
Turning in your answers Please submit your answers on Moodle. You should submit 2 files - Lab1.py containing your answers to Part 2 and a text/word/pdf file containing the answers to parts 1 and 3. Remember to disconnect your network drive so nobody else can see your files! To do this, right-click on the computer icon and select the disconnect command.!5