CPSC120
Fundamentals of Computer Science

Activity 3

Variables

Triangle

What did the triangle say to the circle?

“You’re pointless.”

Details

Create a Python program that uses the turtle module to draw an equilateral triangle perfectly centered on the screen. The program should define, initialize, and use the variable length, which specifies the length of the triangle's edges.

Example

triangle

Hint

  • To draw a triangle, the turtle should move forward and turn three times. The total amount the turtle will turn is 360 degrees. So each turn is 360 / 3 degrees.

  • To center the triangle, the turtle must begin drawing at one of the corners.

  • Moving the turtle to a corner of the triangle requires knowing the width and height of the triangle.

  • Calculating the width is simple. It is half of the edge length.

  • The height of the triangle is a bit more tricky. It can be calculated with the equation:

    $$height = \frac{\sqrt{3}}{2} length$$

Boring Origami

A single sheet of paper is 0.004 inches thick. If you fold the paper in half, the two halves of the paper sheet are together 0.008 inches thick. If you fold the paper in half again, the four quarters are together 0.016 inches thick. How thick would the paper be if you folded it in half 10 times?

Details

Create a Python program that prints how thick a piece of paper would be if you folded it in half 10 times. The program should use meaningful names for values and simple statements to make the program more readable. The output should be nicely formatted and labeled.

Example

The thickness, in inches, of a piece of paper folded in half 10 times is:

4.096

Hint

  • If a single sheet of paper is 0.004 inches thick and folded in half once, it will be twice as thick, 0.004 * 2 = 0.008.
  • If a single sheet of paper is folded in half twice, it will be twice as thick as when it is folded once, (0.004 * 2) * 2 = 0.016.
  • If a single sheet of paper is folded in half three times, it will be twice as thick as if it were folded twice, ((0.004 * 2) * 2) * 2 = 0.032
  • You can probably see where this is going. Just repeat this until you have found the thickness of folding the paper 10 times.

Challenge

How many times do you have to fold a piece of paper in half for its height to reach the moon? Use your program to figure it out. The moon is about 238857 miles from the Earth. The actual distance varies because the moon's orbit is not circular. Change the variable for the number of folds until you find the fewest folds that produce paper taller than the moon’s distance. Note, you will need to convert the thickness to miles or the distance to the moon to inches to make the comparison correctly.

Finding Your Pace

You might think that running has very little to do with Computer Science. However, we can do a lot of useful computations to help a runner figure out how fast they ran. Race distances are typically given in kilometers: 5k, 10k, etc. A fast runner might run a 5K in 20 minutes, but the winning time is usually closer to 15 minutes. In the US, runners typically measure their pace in minutes per mile. We will write a program to perform these computations.

Details

Create a Python program that prints a runner's pace in minutes per mile. Your program should have variables that store:

  • the distance of the race in kilometers
  • the minutes part of their finish time
  • the seconds part of their finish time

The program should use meaningful names for values and simple statements to make the program more readable. The output should be nicely formatted and labeled.

Example

If you run a

5

kilometer race in

27

minutes and

15

seconds, your pace will be

8.790322580645162

minutes per mile.

Hint

  • Begin by defining the variables and constants. You will need variables for the distance in kilometers, distance in miles, minutes part of the time, seconds part of the time, and minutes per mile.
  • Then, compute the distance in miles and the time in minutes. Use the approximation that 5 kilometers is about 3.1 miles.
  • The runner’s pace is their time divided by the distance of the race in miles.

Challenge

Many runners set goals to improve their pace. Add to your program a variable for the user to store their desired minutes per mile. Compute how quickly they would have to run the race, and print this value as well.