yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Calculating weights on Mars with if-elif-else | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

Let's design a program with chain conditionals. We want to build a program that calculates an object's weight on different planets. We have the formula for this already: weight equals mass times gravity.

So, if we know an object's weight on Earth, we can divide by Earth's gravity to get the mass. Then, once we have the mass, we can just multiply by the gravitational force of whatever planet we're interested in. Our starter code here calculates an object's weight on Mars. We take the gravitational force on Mars, which is 3.7 m/s².

Then we ask for the weight of the object on Earth and plug those two numbers into our formula to get the weight on Mars. I'm thinking I want to round this number, though, so it's a bit easier for the user to look at. There's definitely a built-in function for this.

Okay, looks like it's just called round, and it takes two arguments: the number to be rounded and the number of decimal places to round to. That's looking a lot cleaner now. I want to make this work for other planets too, not just Mars.

So, we're going to want an input function call that asks the user to enter a planet name. We'll assign that to the variable planet and then use a conditional to assign the value of gravity. Let's start with the Mars case that we already had before. Before I start my conditional, I want to make sure to initialize my variable gravity to a default value. Let's say zero here; that probably makes sense.

We'll want our condition to be planet == "Mars." If this evaluates to true, then we want to set gravity to 3.7, which is the gravitational force on Mars. So, we indent that assignment statement inside that if branch. There are a lot of planets, so let's just start with a few of them. We'll do Mars, Mercury, Venus, and Jupiter.

Mercury's gravitational force is also 3.7, so we can just use the or operator to turn this into a compound condition that checks if the planet is Mars or Mercury. Next, we want to check if the planet equals Venus. This is related to the previous condition and is mutually exclusive with it; the planet can't both be Mars and Venus. So, it makes sense to use an elif branch here.

If the planet == "Venus," then we want to set gravity to Venus's gravitational force. Then we have the Jupiter case, which is another branch. Now, let's test that conditional. If I enter Mars, it's giving the same weight that I had before, so that must be correct.

Mercury should give the same answer, and then Venus's gravitational force is bigger, so its weight should be bigger. Jupiter should be even bigger still. Oh wait, that's a zero. Maybe that means gravity still has this default value zero when we do the calculation, and maybe this branch isn't actually executing.

Let me test that hypothesis by adding some temporary debugging print statements. I'll add one inside this Jupiter elif branch so I can see if this branch is actually getting executed. Then maybe I'll print down here what the value of gravity is right before I calculate the weight.

Interesting, so it is hitting that branch because it's executing this print statement, and it says that the value of gravity is zero. That's bizarre because if it's executing this branch, it should be executing this assignment statement, which means gravity should be 23.1.

Oh wait, I see the bug. I used the equality operator, not the assignment operator, so this is checking if gravity == 23.1 instead of assigning 23.1 to gravity. Okay, but I need to go in and clean up those debugging print statements.

I don't want my users to see them. What do we want to happen if they enter something that's not a planet at all, or is a typo, or is a planet we just don't support yet? This case is a catchall; it's any string that's not Mars, Mercury, Venus, or Jupiter, which could be any of infinite possibilities.

So, we can't really express that with a separate condition; we need an else branch. Let's just give the user an informative error message and tell them that we don't recognize that planet name. So, we make sure to indent this print statement inside the else branch.

Let's test that else case. It prints "unrecognized planet," but then it prints it would weigh zero kilograms on potato. If the user enters an unrecognized planet, I don't want to print out what it would weigh because it doesn't make sense.

Actually thinking about it, I don't need to do any of these calculations at all. I don't even need to ask them what the weight on Earth is because I'm not going to do anything with it. So, if I want to skip all these lines of code, I'm going to need another conditional.

I don't want to chain it with the existing conditional because it's not mutually exclusive with all those cases. It should run if the planet is Mars, Mercury, Venus, or Jupiter. So, we use an if to start a new conditional, and then I want to indent all of these lines of code inside it.

What should my condition be, though? How do I know if I was in the else case before? Well, if none of these three branches executed, then the value of gravity would still be zero. So I can have this condition be if gravity != 0. But I can actually use a shortcut here because zero cast to a Boolean is false, and any other float cast to a Boolean is true.

So, I can just shorten this condition to if gravity, which checks that gravity has some value that's not zero. Now if I enter some unrecognized planet name, it just gives me that error message and then skips all of the calculations.

Break time! I'm going to add a quick comment here so I remember where to pick up later. I'll note down the gravitational forces of the remaining planets in case you want to give it a try.

More Articles

View All
Become Who You're Afraid To Be | The Philosophy of Carl Jung
Most people are afraid to fully be themselves. They’re afraid to embrace the parts of themselves that might be regarded as unacceptable because embracing these unacceptable parts makes them feel uncomfortable. So, to escape this uncomfortableness, they di…
Vertices & direction of a hyperbola | Precalculus | High School Math | Khan Academy
Which of the following graphs can represent the hyperbola ( \frac{y^2}{9} - \frac{x^2}{4} = 1 )? We have our four choices here. Choices A and C open up to the top and the bottom, or up and down. Choices B and D, you can see, D here opens to the left and …
Why I DON'T flip houses (revealing my favorite real estate investing approach)
What’s up you guys, it’s Graham here. So one of the questions I get asked a lot is, am I going to be flipping this place or am I going to be selling it in the short term? The answer is no. In fact, of all five places I bought, I’ve never once wanted to se…
Rule of 70 to approximate population doubling time | AP Environmental Science | Khan Academy
When we’re dealing with population growth rates, an interesting question is how long would it take for a given rate for the population to double. So we’re going to think about doubling time now. If you were to actually calculate it precisely, mathematica…
How to Fight Fire or Flooding on a Nuclear Submarine - Smarter Every Day 244
Hey, it’s me, Destin. Welcome back to Smarter Every Day. Earlier this year, I had an amazing opportunity to board a U.S. Navy nuclear submarine on an ice flow in the Arctic. This is the next video in a Smarter Every Day deep dive series into submarines an…
Share your career story with Khan Academy for our new series
Hi, I’m Sal Khan, founder of the Khan Academy, and I’m here to invite you to participate in an exciting project that we have around career. Our mission statement as a not-for-profit is to provide a free, world-class education for anyone, anywhere, and par…