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.
Please do round all results that are floats to two digits after the decimal point. You can use Python's round function for that.
Finally, let me answer a question I often get about Python's print function. The question is this: If I print multiple comma-separated objects, how do I get period at the end of a line and yet not have a space before the period? You can't do that like this: print(2, "."). That'll put a space before the period, since Python by default inserts a space between any two objects that are printed. Try this instead: print(2, ".", sep = ""). That tells Python to separate objects by the empty string. (Notice that the open and close quotes after sep have nothing between them.) Another way to do it is this: print(2, end = ".\n"). This tells Python to end the output with a period followed by a newline. (The newline is given by "\n". This is called an escape sequence, which we'll take up in greater detail in the chapter on strings.) Read more here.
Assign the variable Fahrenheit a value of 0.
Begin a second line with Celsius = . On the right, construct an expression that contains Fahrenheit and converts its value to Celsius. (You'll likely need to do a Google search for the conversion formula.)
Print the result with a helpful message. Like "A temperature of 0 Fahrenheit is equivalent to ... Celcius".
Do what you did above but give Fahrenheit a value of 100 to begin. Don't forget the helpful message.
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.)
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.
Now do my dog's age in seconds. Assume that she's 9.
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.)
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 %.)
I discussed Python's input function in Chapter 1. See here. The line of code below is the one we used to ask the user to input an integer which would then be squared. Note that the input was converted to integer type.
to_square = int(input("Give me an integer and I'll square it: "))
When executed, this line of code will display the message "Give me an integer and I'll square it: "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.
We could also ask the user to input a value that would become a float. Here we'd use float instead of instead int to do the type conversion. Example
to_square = float(input("Give me a float and I'll square it: "))
Here's your task: write code that will ask the user to input the radius of a sphere and will then print the surface area and volume of that sphere along with a nice message. The input should become a float. You should be able to recycle code from above.
The standard form of a linear equation is ax + by + c = 0. If values are picked for a, b and c, a linear equation results. For instance, if -2, 3 and 5 are picked for a, b and c respectively, the linear equation that results is -2x + 3y + 5 = 0.
Let's say that we've picked values for a, b and c, and that we also choose a value for x. We can then solve for y. For instance, if we choose a value of -7 for x in the linear equation
-2x + 3y + 5 = 0, we get -2 ⋅ -7 + 3y + 5 = 0; and if we solve this for y, we get y = -19/3 ≈ -6.33.
Write code that uses the above values for a, b and c (-2, 3 and 5 respectively), asks the user to input a value for x, and prints the y value that corresponds to this x. Inform the user of the values of a, b and c, and print the value of y that corresponds to the given x along with a nice message. (See my output below for a suggestion about how to do this.)
The input value should be converted to a float.
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 surface area 452.39 and volume 904.78 if is its radius is 6.
A sphere has surface area 4071.5 and volume 24429.0 if is its radius is 18.
The third digit from the right in 13579 is 5.
The third digit from the right in 135798642 is 6.
Please input the radius of a sphere: 3.2
The surface area of a sphere of radius 3.2 is 128.68.
The volume of a sphere of radius 3.2 is 137.26.
Please input a value for x: -7
If we solve for y in the equation -2x + 3y + 5 = 0 when x = -7.0 we get -6.33.