CPSC120
Fundamentals of Computer Science

Activity 5

Input

Squared

Now that we know how to use the input function, we can test our programs without making any changes to the code. Your homework assignments will use automated testing to give you instant feedback on your code. This is great because it will allow you to fix errors right away. To practice with HumblePython’s automated testing, complete the Squared programming challenge.

Take note! The program you are writing will be automatically tested by another computer program. As such, it doesn’t need to be designed to be easy for a person to use. So, the program should not print anything to prompt for input. And the program does not need to label the output. The program should read the input and print the output exactly as shown in the examples.

Military Time

The 12-hour clock can be confusing to computers. You want to add a dinner date for 7 o'clock into your calendar. You know that this must be 7PM because to have dinner at 7AM would be lunacy. But your phone doesn’t know that. Fortunately, converting between the 24-hour clock that computers like and the 12-hour clock we use is simple.

Details

Create a Python program that converts a 24-hour clock time to 12-hour clock time. The program should prompt the user for an integer between 0 and 23 and print the corresponding 12-hour clock time. The program should replace 12 o'clock with 0 o'clock to make the conversion more simple. The calculations should be performed using Python's arithmetic operations with variables and printed nicely labeled and formatted.

Example

Enter a time as an integer between 0 and 23:

19

That is 7 o'clock.

Enter a time as an integer between 0 and 23:

0

That is 0 o'clock.

Hint

  • Both the input and output times of the program are whole numbers, so declare int variables for each of them.
  • Get the user’s input using the input function. Remember, the input function returns a str, but the variable you declared is an int. So the str value must be converted using the int function before it can be assigned to the variable.
  • You can use the remainder operator, %, to convert the input time to a 12-hour clock time. If on a 12-hour clock, all numbers are between 0 and 11, then computing the remainder of dividing by twelve will always produce numbers in the correct range.

World's Largest Ice Cream Cone

The world's most massive ice cream cone was 9 feet tall, according to the Guinness Book of World Records. While impressive based on the height alone, one has to wonder how much ice cream actually made such an atrocity. Math is, of course, our friend, but writing a program could make it easier to figure out how big of an ice cream cone you and your friends would have to make to beat the current record.

Details

Create a Python program that prompts the user to enter the height and radius of an ice cream cone, in feet, and prints the volume, in cubic feet. Assume that the entire cone is filled with ice cream and that there is a perfect half-sphere of ice cream on top of the cone, like this:

ice_cream

The volume of a cone can be computed using the equation:

$$v = \pi \cdot r^{2} \cdot \frac{h}{3}$$

Where \(v\) is the volume of a cone, \(r\) is the radius of the cone, and \(h\) is the height of the cone. The volume of a sphere can be computed using the equation:

$$v = \frac{4}{3} \cdot \pi \cdot r^{3}$$

Where \(v\) is the volume of a sphere, and \(r\) is the sphere’s radius. 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

Enter the height of the cone in feet:

9.0

Enter the radius of the cone in feet:

2.0

The volume of the cone is 37.69911184307752 cubic feet.

The volume of the half-sphere is 33.510321638291124 cubic feet.

The volume of ice cream is 54.454272662223076 cubic feet.

Hint

  1. First, create variables for the input values to the program. In this case cone_height and cone_radius are the inputs to the program. Don't forget to convert the input from a string to a float!
  2. Next, create variables for the computations that will be performed. In this case cone_volume, sphere_volume, and ice_cream_volume. Set each of these variables equal to arithmetic expressions that compute the correct value. Note that the ice_cream_volume variable depends on the other two variables, so it must be defined after them.
  3. Finally, create print statements that print the calculated values nicely formatted.

Challenge

So how many children can the world's most massive ice cream cone feed? Modify your program to calculate and print the result. Assume that a single serving of ice cream is 4 oz and note that there are 996.6124 oz in a cubic foot.

Enter the height of the cone in feet:

9.0

Enter the radius of the cone in feet:

2.0

The volume of the cone is 37.69911184307752 cubic feet.

The volume of the half-sphere is 33.510321638291124 cubic feet.

The volume of ice cream is 54.454272662223076 cubic feet.

The ice cream cone will feed 13567 kids.