Week 2 Labs:


In these labs, you will ...

NOTE NOTE NOTE: for all these labs, you should work with a partner. You will submit only one copy of your work, with both your names and userids in the comments at the top of the file. The graders will apply the grade to both of you in Moodle.

Part1: Computing Circumference and Area

Summary: Write a program to ask the user for a diameter in inches. Compute and print out the circumference of a circle with that diameter, in centimeters, followed by the area of the circle, in square centimeters. Make sure your output is nice and readable (i.e., don't just print out numbers.)

Step 1: Make the lab2 folder and area.py file.

Open Thonny. Make sure you are using S:\CS104 for saving your work. Create a new folder called "lab2" in your S:\CS104 directory. Then, save and create a new file area.py. within Thonny.

Step 2: Write the code to get user input

To get a value from the user running the program, you can call this function:

result = input("Some string to display here:")

The result is always a string, which is not what you need for this assignment. You can convert the string to a floating point number like this:

result = float(input("Some string to display: "))

Now, result will be a floating point number.

Write code to get the diameter of a circle from the user. NOTE: you are asking them for the diameter in inches, but you have to print out your values in centimeters and square centimeters. (FYI: there are 2.54 centimeters per inch.)

Step 3: Calculate the circumference and area and print them.

Now, write code to compute the circumference and area of a circle with the given diameter. Your code should print the results. Make sure that you print out a nice message for the user. Don't just print out two numbers. We want to write user-friendly programs.

Step 4: Submit your code

Before you submit your code, make sure that this information is in a comment block at the top of the file area.py:

#
# CS104, Lab 2: Compute Circumference and Area.
# <Your Name and your partner's name, if you have a partner, and your login ids>
# <Date>
#

Also, to get full credit, you should put comments in your code, as appropriate. E.g.,

# Get values from the user.

Use good variable names that will mean something to the reader, without being too long. Think of others when you write your code: what will help them understand your code.

To submit your final version of area.py, you’ll need to use Windows Explorer.


This lab is worth 5 points. 
2.5 points: program that runs correctly -- producing correct results.
1 points: correct comments at the top of the file.
1.5 points: code is clean and neat and perfectly indented, with good variable names, comments, etc.

 

Part 2: Convert Number of Days to Number of Weeks and Days

Summary: Write a program that asks the user for a number of days, and prints out how many weeks and days that is. E.g.,

Enter a number of days: 44
44 days is 6 weeks and 2 days.

Step 1: Make the weeks.py file.

In Thonny, in your lab2 folder, create a new file weeks.py.

Step 2: Write the code to get user input

Write code to get the number of days from the user.

Step 3: Calculate the results and print them.

Now, write code to compute the days and weeks. Make sure that you print out a nice message for the user. Don't just print out two numbers. We want to write user-friendly programs. See the example output above in the Summary for this part.

Step 4: Submit your code

Before you submit your code, make sure that this information is in a comment block at the top of the file weeks.py:

#
# CS104, Lab 2: Compute Weeks from Days
# <Your Name and your partner's name, if you have a partner, and your login ids>
# <Date>
#

Also, to get full credit, you should put comments in your code, as appropriate. E.g.,

# Get values from the user.

Use good variable names that will mean something to the reader, without being too long. In short, think of others when you write your code: what will help them understand your code.

To submit your final version of weeks.py, you’ll need to use Windows Explorer.


This lab is worth 5 points. 
2.5 points: program that runs correctly -- producing correct results.
1 points: correct comments at the top of the file.
1.5 points: code is clean and neat and perfectly indented, with good variable names, comments, etc.

 

Part 3: Computing the period of a pendulum

The period of a pendulum is given by the formula

period =

where g = 980 cm/sec-squared, L = pendulum length (cm), and alpha = angle of displacement (radians). Write a program to read values for L and alpha for a pendulum, and then calculate and display its period. Execute your program with the following inputs:

L (cm) alpha (radians)
120 0.1
90 0.5
60 1
74.6 0.01
83.6 0.7

Step 1: Make the pendulum.py file.

In Thonny, in your lab2 folder, create a new file pendulum.py.

Step 2: Write the code to get user input

Write code to prompt the user to provide the pendulum length first, which you'll store in variable L. Then, ask for the angle of displacement, which you'll store in variable alpha.

Write code to print out the values you got from the user -- just to test that your code so far is correct. Run your code and see that it works.

Step 3: Calculate the result and print it.

Now, write code to compute the period of a pendulum using the values read in from the user.

To use the sin() function you need to import the math module. To do that, put this code

import math

up near the top of the file.

Then, you can call the sin() function this way:

math.sin(somevalue)

Your code should print the result. Make sure the output is pretty.

Step 4: Run your code

Test your code by running it 5 times, using the 5 sets of inputs given above in the table. After each run, put the results into a file called results.txt. (You can create this file in your lab2 folder in the File Explorer. Just make sure you create a text file, not a python file, and make sure the filename ends with .txt.) You will submit this file with your code file.

Step 5: Submit your code and results

Before you submit your code, make sure that this information is in a comment block at the top of the file pendulum.py:

#
# CS104, Lab 2: Pendulum Period
# <Your Name and your partner's name, if you have a partner, and your login ids>
# <Date>
#

Also, to get full credit, you should put comments in your code, as appropriate. For example, you should comment what your variable g stands for. You might also put a comment above each section of code, describing what it does. E.g.,

# Get values from the user.

Use good variable names that will mean something to the reader, without being too long. In short, think of others when you write your code: what will help them understand your code.

To submit your final version of pendulum.py, you’ll need to use Windows Explorer.


This lab is worth 5 points. 
2.5 points: program that runs correctly -- producing correct results. (If you don't submit results.txt, you lose 2 points.)
1 points: correct comments at the top of the file.
1.5 points: code is clean and neat and perfectly indented, with good variable names, comments, etc.