Chp 1 Program

Instructions

Hop into your code editor and write one (count 'em - ONE) program that carries out each of the tasks described below.  The output will be a sequence of numbers and/or strings, one for each task. Please name the file that contains the program "Chapter 1".

I know the code I ask you to write will seem repetitive. Trust me. I made it this way for a reason, a reason I'll reveal in the next chapter.

Note that below the program descriptions, I show you the output from my code. Compare yours to it.

If you decide that you'd like to use some function from Python's math module, you'll have to include the line import math in your program. Put that up top.

Finally, please do round all results that are floats to two digits after the decimal point. You can use Python's round function for that.

0° Fahrenheit to Celsius

100° Fahrenheit to Celsius

Do what you did above but give Fahrenheit a value of 100 to begin. Don't forget the helpful message.

Your Age in Seconds

Take your age in years and converts that to your age in seconds. Use these variables: age_in_yrs, secs_in_min, mins_in_hr, hrs_in_day, days_in_yr. Your code should begin:

age_in_yrs = 

secs_in_min = 60

(You'll of course fill in your age in years.)  Continue on. The last of the assignment statements should begin:

age_in_secs = 

A calculation should appear on the right, a calculation that uses all of the variables assigned a value above.

End with a statement that prints the value of age_in_secs. Don't just print the value; print it with a helpful message.

(I know that some years have 366 days. Ignore this. Pretend that every year is precisely 365 days.)

My Age in Seconds

Now do my age in seconds. Assume that I'm 51. You'll reuse some of the variables from the previous program; that means you don't have to define them again.

My Dog's Age in Seconds

Now do my dog's age in seconds. Assume that she's 9.

Sphere Surface Area and Volume

Begin with the line radius = 6.  Compute the surface area and volume of a sphere with that radius and store those values away in variables. (I'll let you choose how to name those variables. Make them describe the values they name!) Print those values together with a helpful message for the user.

Do it again, but now use a value of 18 instead of 6. Don't forget the helpful message.

I suggest that you define a variable PI and use that. What precision do I want? I don't really care. I used 3.14159.

("But Dr. Mason! I don't know the formulas!" Dr. Mason replies, "Perhaps you've heard of Google", with just a hint of sarcasm in his voice.)

Third Digit

Begin with the line:

an_int = 13579

Perform a calculation (or set of calculations) that will extract the 3rd digit from the right (which is this case is 5). Store the result away in a variable. Print the result.  (Hint: // and %.) The output should take the form: "The third digit of 13579 is ...'.

Now extract the the third digit of another_int, where another_int = 246810. Print a complete sentence as above.

Did you do different calculations for the two? If so, find another way; that is, find a way that will work for both values. Indeed your method should work for any positive integer. Seem impossible? I assure you that it is not. (Again the hint: // and %.)

Input Radius

We discussed Python's input function. The line of code below is the one we used to ask the user for the number of sides of a polygon and then store that value away in the variable num_sides:

num_sides = int(input("What is the number of sides? "))

When executed, this line of code will display the "What is the number of sides?" message in the console and then wait for the user to input a value. As we said, the input function will return a string; the function int then takes that string and converts it into an integer.

Now, I want you write a last chuck of code that will ask the user to input the radius of a sphere and will then print the volume of that sphere with a nice message. You should be able to recycle code from above.

My Output

I think it might be helpful if you see the output from my code. Your messages don't have to be identical to mine, but your numbers should be the same or very close to mine.

A temperature of 0 Fahrenheit is equivalent to -17.78 Celsius.

A temperature of 100 Fahrenheit is equivalent to 37.78 Celsius.

I am 51 years old. This is equivalent to 1608336000 seconds.

My dog is 9 years old. This is equivalent to 283824000 seconds.

A sphere has volume 904.78 if 6 is its radius.

A sphere has surface area 4071.5 if 18 is its radius.

The third digit from the right in 13579 is 5.

The third digit from the right in 135798642 is 6.

Input the radius of the sphere: 12

A sphere has volume 7238.22 if 12 is its radius.