To understand just how exact you need to be with Python, play with the line:
print(5.0**2)
What kind of errors do you get when you make each of these mistakes? (All you need to do right now is copy the code above, make the changes, run the cells and look at the error messages.)
**
with ^
:
2. Use write
in the place of print
:
3. Use Print
(with a capital P) in the place of print
:
Just like we did above, we're going to see what happens when we try to break some of Python's rules. For both tests, you will start by copying the code below:
Solar_Flux = 1366
Albedo = 0.3
Boltzmann_Constant = 5.67e-8
Effective_Temperature = ((Solar_Flux/(4*Boltzmann_Constant))*(1-Albedo))**0.25
print(Effective_Temperature)
What happens if you:
Albedo
to albedo
the second time you use it (but not the first time):
2. Add a space instead of an underscore to the name for the variable representing solar flux (so it is now called Solar Flux
instead of Solar_Flux
):
Just to bring the point home, fix the programs below so that they don't give an error message. (Use any value you want for x)
# Original
print(x+2)
# Fixed
# Original
y=4
print(y+z)
z=2
#Fixed
We're going to put everything we have learned so far into practice with an example that is relevant to what we are learning about in the lecture this week.
Your goal is to write a simple program to solve the problem. Your program should include variables with understandable names and comments that explain every step of what you are doing.
A so-called "blackbody" is something that emits (and absorbs) radiation with 100% efficiency (see textbook, page 55). Wien's Law states that "the flux of radiation emitted by a blackbody reaches its peak value at a wavelength $\lambda_{max}$, which depends inversely on the body's absolute temperature" (textbook, page 56). If $T$ is the temperature in Kelvin, $\lambda_{max}$ in microns (10$^{-6}$ m) is calculated as:
$$\lambda_{max} = \frac{2898}{T} $$
The surface temperature of the sun is about 5780 K and the surface temperature of the Earth is about 288 K. Write a program to calculate the wavelength of maximum radiation for both the sun and the Earth. Your answer can be in either microns or meters, but make sure your comments tell us which one you've used!
Copy your solution from Exercise 2 here, and modify it so that at the end it prints out not just the final answer but also which body (sun or Earth) and what the units are.
Given a quadratic equation, $ax^2 + bx + c = 0,$
we can find solutions for the values of x:
and
$$x2 = \frac{−b−\sqrt{b^2 −4ac}}{2a}.$$
Note that the $\sqrt{x}$ is the same thing as $x^{0.5}$.
Why does the following program not work correctly?
Identify the errors AND use comments to create a version of the program that does run correctly.
If you have fixed ALL the problems, you should find that the values for x1 and x2 are roughly 0.618 and -1.618 (you can verify this using a calculator, for example: https://www.calculator.net/quadratic-formula-calculator.html). If you're getting different numbers, that means there are still some *logic errors* that you haven't fixed. Hint triple check that you have the brackets in the right place!
# Original Program
a = 2
b = 2
c = -2
q = b**2 - 4ac)**0.5
x1 = -b + q/2*a
x2 = -b - q/2*a
Print(x1, x2)
# Fixed Program
Now try to use your new and improved program with
a = 0
b = 1
c = 2
Why do you get an error? What does that error mean?
# Fixed Program with new values
This final exercise is designed to give you a chance to practice everything you have learned so far today - and also get you into the practice of thinking about what your answers mean.
Remember from the start of the practical (or the lectures, or the textbook) that the temperature a planet would have without an atmosphere is called the effective temperature of a planet ($T_e$, in Kelvin) and can be calculated from the formula: $$T_e = \left (\dfrac{S}{4\sigma}*(1-A) \right )^{0.25}$$
where:
Using the parameters in the table below, write a program that:
Planet | Solar Flux (W/m$^2$) | Albedo |
---|---|---|
Venus | 2643 | 0.8 |
Earth | 1366 | 0.3 |
Mars | 593 | 0.2 |
*Do not delete the %reset in the cell below* (this makes sure your program isn't just "remembering" something you did before).
%reset -s -f
# Program for computing the effective temperature of a planet
# YOUR CODE STARTS HERE
The actual surface temperatures are
Which planet shows the smallest difference between the effective temperature (without an atmosphere) and the actual temperature (with an atmosphere)? Which shows the biggest difference? What can you infer about the atmospheres of these three planets based on these differences?
# Type your answer here, as comments
# Use as many lines as you need to make it readable
#
#
We can measure the solar flux at Earth. But what if we want to know the solar flux at another planet? We can find the solar flux S anywhere else in the solar system using the inverse-square law (see textbook, page 54), expressed as:
$$S = S_0*\left (\frac{r_0}{r}\right ) ^2$$
where $S_0$ is the known solar flux at a known reference distance from the sun $r_0$ (here, the distance of Earth) and $r$ is the distance from the sun where we would like to calculate the solar flux (in the same units as $r_0$. The distance from the Earth to the Sun is 1.00 AU (AU is an "astronomical unit" defined as 149600000 km), and the solar flux at Earth is 1366 W/m$^2$.
Write a program that computes the solar flux at a specified planet. Your program should:
Use the program to calculate the solar flux at:
Make sure you use the formatting statements introduced in the optional extras in the lab.