Goals
Upon successful completion of this lab, you should be able to write and edit Python programs that use:
- numeric data types
- arithmetic operations
- order of operations
- the math library
Setup and Practice
Setup
- If your lab machine was not started in MacOS when you went to use it, get help to reboot it to MacOS.
- Follow these procedures to mount your blue home-directory on the local machine and create a PyCharm project (call it Lab01). You will use PyCharm in the next part, but, it is important that you set up your project now so that if you run into any issue, we can resolve them quickly.
Practice
Instructions
- Go to your browser tab with the Lab 1 writeup in Moodle, and answer Questions 1-4
- Now enter the following code in the online Python tutor:
x = "I" y = 'see' x = "what" x = 'you' y = "did there!"
Then click "Visualize execution." - Try to answer Question 5 in your Moodle writeup based on what you observed. If you get stuck, you can click the "First" button in the Online Python Tutor to restart the visualization.
- Continue to Part A.
Part A: Computing the area of a square
The instructions below will help you write a program that:
- Asks the user to enter the side length for a square.
- Calculates the area of the square.
- Prints that calculated area.
You should be able to trace through the execution of this program and understand how the values of the variables change after each statement.
Note: For full credit in the lab, your output will have to match the final sample outputs exactly.
Instructions
- Answer Questions 6-8 in Moodle.
- Create a new Python program called lab01a.py in project cs115/lab01.
- Type (or copy-paste) the following program exactly as written, substituting your name for the italicized text:
""" Program: CS 115 Lab 01a Author: Your name Description: This program will compute the area of a square, given the side length. """ def main(): # Get the side length length = input('Enter a numeric value: ') # Compute the area of the square square_area = length * length print("The area of a square with side length ", length, " is ", square_area, ".", sep="") main()
-
The above program has one error. The variable
length
is not of the correct datatype (What datatype is it?). Fix the error by converting it tofloat
datatype. - Execute the program several times to complete the table in Question 9 of your Moodle writeup.
- Each time you execute the program, you will be prompted to enter a value. When that happens, type in the information from one of the rows, exactly as it appears in the Input Value column, and then press the enter key.
- Record the value that your program reports for the area of the square.
- Continue to Part B.
Part B: Computing the volume of a cube
In this part of the lab, we will extend our program from Part A, to compute the volume of a cube, given the length of the side of cube.
Recall that the volume of a cube with side length s can be calculated as
V = s3.
Here is a sample of what your revised program will do. The user's input is italicized and underlined.
Enter a numeric value: 10 The area of a square with side length 10 is 100.0. The volume of a cube with side length 10 is 1000.0.
Instructions
Save lab01a.py as lab01b.py. In PyCharm, this can be done by clicking on the "File" menu on top, and then clicking on "Save As..". Work on the file lab01b.py from this point onwards.
Since you are extending your program, do not delete any of your existing code as you follow the instructions below. Also, do not prompt the user to enter another value.
- In your current program, notice that the statement
square_area = length * length
computes the area of a square whose side length islength
and saves the result in a variable calledsquare_area
. - Just after that statement, add a similar statement to your program to calculate the volume of a cube whose edge length is
length
. Store the result of this calculation in a new variable with an appropriate name. - After your current print statement, add a statement to the program to print the volume of the cube. This statement should be similar to the print statement that prints the area of the square, but be sure to change the wording to match the sample output above.
- Run your program. Make sure it matches the example below:
Enter a numeric value: 10 The area of a square with side length 10 is 100.0 The volume of a cube with side length 10 is 1000.0
- Run your program. Record the volumes of the cubes in Question 10 of your writeup.
- Continue to Part C.
Part C: Computing the volume of a sphere
In this part of the lab, we will extend our program from Part C, to compute the volume of a sphere, given the length of the radius.
Recall that the volume of a sphere with radius r can be calculated as:
V = 4/3 * π * r3
Here is a sample of what your revised program will do. The user's input is italicized and underlined.
Enter a numeric value: 10 The area of a square with side length 10.0 is 100.0. The volume of a cube with edge length 10.0 is 1000.0. The volume of a sphere with radius length 10.0 is 4188.79.
Instructions
Save lab01b.py as lab01c.py and work on lab01c.py from this point onwards. Remember: not to delete any of the existing code. Also, do not prompt the user to enter another value.
- Just after the other calculations, add a line to the program to calculate the volume of a sphere whose radius length is
length
. Store the result of this calculation in a new variable with an appropriate name. Hint:math.pi
stores the value of π. - Add a line to the program to print the volume of the sphere. Be sure the wording matches the sample output.
- Run your program. Compare your results with the output below:
Enter a numeric value: 10 The area of a square with side length 10.0 is 100.0. The volume of a cube with edge length 10.0 is 1000.0. The volume of a sphere with radius length 10.0 is 4188.79.
Modify your program to make sure it matches the sample output above, exactly. It may be the case that your output looks different compared to the above. If so, employ the below guidance to make it match.
First, if the decimal places of the area of a square do not match: it may be that variable
length
holds anint
, when it should be afloat
. Adjust your code so that variablelength
holds the correct type of value.Second, if the decimal places of the area of a circle do not match: it may just be that the computer you are on has a different floating point accuracy, and you need to round the value to three decimal places to match the output. The
round()
function is used to round numeric values. For example, the below code rounds the value 2.6757 to 3 decimal places, and produces the output The value is 2.676.print("The value is", round(2.6757, 3))
- Run your program. Record the volumes of the spheres in Question 11 of your writeup. Make sure you have rounded your output to 3 decimal places.
- Continue to Part D.
Part D: Computing the area of an equilateral triangle
In this part of the lab, we will extend our program from Part C, to compute the area of an equilateral triangle, given the length of a side.
Recall that the area of an equilateral triangle with side s is:
s2 * (square root of 3) / 4
Here is a sample of what your revised program will do. The user's input is italicized and underlined.
Enter a numeric value: 10 The area of a square with side length 10.0 is 100.0. The volume of a cube with edge length 10.0 is 1000.0. The volume of a sphere with radius length 10.0 is 4188.79. The area of an equilateral triangle with side length 10.0 is 43.301.
Instructions
Save lab01c.py as lab01d.py and work on lab01d.py from this point onwards.. Remember: not to delete any of the existing code. Also, do not prompt the user to enter another value.
- Just after the other calculations, add a line to the program to calculate the area of an equilateral triangle whose side length is
length
. Store the result of this calculation in a new variable with an appropriate name. Hint:math.sqrt(x)
will compute the square root of the value stored in variable x. - Add a line to the program to print the area of the equilateral triangle. Be sure the wording matches the sample output exactly.
- Run your program. Make sure it matches the example:
Enter a numeric value: 10 The area of a square with side length 10.0 is 100.0. The volume of a cube with edge length 10.0 is 1000.0. The volume of a sphere with radius length 10.0 is 4188.79. The area of an equilateral triangle with side length 10.0 is 43.301.
It should match in terms of spelling, capitalization, white-space and decimal precision. If your output's decimal places do not match the above, then review the guidance from Part C on how to match the decimal output. - Run your program. Record the areas of the triangles in Question 12 of your writeup.
- Update your docstring to reflect what your program is doing now.
- Demo. Call an instructor or lab assistant over to demo your program.
- Continue to submission.
Assignment Submission
Instructions
- Answer Question 13 in your Moodle writeup. Review your answers, and then click the "Next" button at the bottom of the quiz. Once you do that, you should see something similar to this (note, number of questions may be different in your writeup; just make sure all questions have been answered):
Click the "Submit all and finish" button. Warning: You must hit "Submit all and finish" so that your writeup can be graded! It is not submitted until you do this. Once you have submitted your quiz, you should see should see a dialog box similar to one shown below. The important part is that the "State" is recorded as Finished.
Please leave this window up as a tab in your browser.- Click on the "Lab 1 code" link in Moodle. Follow the instructions to upload your source code (lab01d.py) for Lab01. You could either browse for your code or, using a finder window, drag and drop your lab01d.py from your cs115/Lab01 folder to Moodle. You should subsequently see a dialog box which indicates 'Submission Status' as 'Submitted for grading' similar to the box shown below.
Warning: Ultimately, it is your responsibility to ensure you have submitted all the parts of the lab correctly. You might want to double-check everything is submitted, and also backed up.
Optional: For your peace of mind, you can call over the instructor or a student assistant and have them verify your confirmation screens and let you know if it looks like you submitted all the parts of the lab and that your demo got recorded. We are happy to be a second set of eyes for you.
If you are confident that everything is completed, you may close your tabs, log out of your machine and leave lab.