# FAQ: Control Flow - Review

You can’t use an expression with `else`, it should be just `else:` on its own followed by a suite of code-

``````else:
do something else
``````

That would also match your text description better as you want this to occur should the input be anything other than 1, 2, 3, 4, 5 or 6. If you wanted your expression to work `planet >= 7` then use another `elif` instead.

1 Like

1 Like

This is what I have. Why doesn’t the else function work here?

print("I have information for the following planets:\n") print(" 1. Venus 2. Mars 3. Jupiter") print(" 4. Saturn 5. Uranus 6. Neptune\n") weight = 185 planet_three = "Jupiter" planet = 3 # Write an if statement below: if planet == 1: weight = weight * 0.91 elif planet == 2: weight = weight * 0.38 elif planet == 3: weight = weight * 2.34 elif planet == 4: weight = weight * 1.06 elif planet == 5: weight = weight * 0.92 elif planet == 6: weight = weight * 1.19 print("Your weight is " + str(weight) + " on planet " + str(planet) + (".")) else: print("Invalid entry. Try again.")

Also if I want to name the planets and specify the planet name instead of number, how do I go about setting it up?
Sample output: Your weight on planet [name] is: [weight].

I know I can setup variables like planet_three = earth but how do I go about showing back to the user their input through the name instead of number?

Thanks.

Line 22 seems to have unmatched indent.

Hi! I tried using a return statement in the If and Elif statement but kept getting an error. Can someone help me understand why?

Using `return` outside the body of a function is not possible, its’ just invalid syntax.

Got it! Thank you so much!

1 Like

Hello! When I run space.py, the result for planet 4,5,6 gets printed with 14 decimal places. It works fine for planets 1,2,3. It’s the same for the solution code. Can anyone clear up for me why this is happening?

1 Like

It’s to do with floating point numbers. The following links might be useful to you but it’s a well known issue with lots of useful guidance floating around.

https://docs.python.org/3/tutorial/floatingpoint.html#representation-error

It’s well worth reading up on this (both as an issue affecting accuracy and representation). The Python link should have some options for dealing with the representation side and bits to help with accuracy should that ever become important.

In terms of representing numbers something like the built-in round function or string formatting might be enough for your purposes, for now at least.

Thank you for your help. I’m having the hardest time comprehending why it does it only for the last 3 planets and not for all of them. I understood most of what the wikipedia article is about so for now I reckon it’s enough for me to move forward without obsessing over it If your solution differs from the LE solution, it could be something minor. Please show us your code.

I’m brand new to programming, what are the disadvantages of writing my code for this as below? Aside from having to type a little more, especially if I wanted to add a string “Your weight is” for example to each print statement, the code seems functionally the same. Why should this be done as in the example solution, by updating the weight variable every time and printing at the end, rather than just having the print statement in every elif?

print("I have information for the following planets:\n") print(" 1. Venus 2. Mars 3. Jupiter") print(" 4. Saturn 5. Uranus 6. Neptune\n") weight = 185 planet = 3 # Write an if statement below: if planet == 1: print(weight * 0.91) elif planet == 2: print(weight * 0.38) elif planet == 3: print(weight * 2.34) elif planet == 4: print(weight * 1.06 ) elif planet == 5: print(weight * 0.92) elif planet == 6: print(weight * 1.19)

In that specific example I’d be tempted to update or reassign another name to `weight` because it can then be used for something else.

With just `print` on each suite of code all that code can do is print output. If you adapt weight then you can further use that value, e.g. create a nice formatted string, do silly additional calculations like how high can you jump on Mars and so on and still print if you want.

When functions are introduced this may become clearer. If the function returns a single useful value we then have a lot of choice on what to do with it. Some functions may still be restricted to just printing but just keep code modularity in mind, how reusable is the code you create?

1 Like

That is an exceptional answer, thankyou very much for taking the time to help. 1 Like

I had a quick question for this exercise. How come we can set weight = weight * the planets relative gravity? Because in my head, the computer already knows that the planet’s weight is = 180. So, wouldn’t what’s typed out really be 180 = 180 * the planets relative gravity which is not true. Wouldn’t you have to set the variable up to be a different name rather than weight?

If weight on Earth is 100, then the weight on another planet will be proportional to it, based on the ratio of their mass (and the gravity that arises). Weight varies with gravity, while mass remains constant.

Not really. The computer doesn’t actually know anything. Our program might know because it’s been told, but the computer? Nata.

The gravity on Earth is a direct result of its mass. Given we’re on the same planet, our mass and weight have a one to one relationship so it’s fair to say that if one weighs 50kg, their mass is also 50kg. Technically, that is, Earth mass at rest.

When we go to another planet, our mass does not change. Only our weight changes, owing to the effect of gravity on that planet. If the planet has more mass than Earth, we will weigh more. If less, then we will weigh less.

The proportion is all based upon the relative masses of the two planets. That will tell us whether to expect more G-force, or less, and will be the determining factor in our weight.

Gravity is the same everywhere in space, as far as we can tell. In localized space, this is especially so. There are no anomalies. That’s what makes these ratios so reliable.

As for variables. they are just names. View them as pointers to data objects. It’s the objects that have value, not the variables. Values cannot be variables. That’s why the interpreter will burp when we try to write something like this,

``````180 = 180 * planetary_mass_ratio
``````

We broke two rules here, one already mentioned, the other that variables can never start with a number.

``jupiter_weight = earth_weight * ratio_of_jupiter_m_to_earth_m``
1 Like

Thank you very much.

1 Like

Hi all- I am doing the optional space exercise a different way. Can someone please tell me what I am doing wrong?

if planet == 1:

return 0.91 * str(weight)

elif planet == 2:

return 0.38 * str(weight)

elif planet == 3:

return 2.34 * str(weight)

elif planet == 4:

return 1.06 * str(weight)

elif planet == 5:

return 0.92 * str(weight)

elif planet == 6:

return 1.19 * str(weight)

else:

print (“NA”)

All the returns are indented to be part of the function. But I receive this error: File “space.py”, line 12
return 0.91 * str(weight)
^
SyntaxError: ‘return’ outside function

``````>>> 0.91 * str(100)
Traceback (most recent call last):
File "<pyshell#75>", line 1, in <module>
0.91 * str(100)
TypeError: can't multiply sequence by non-int of type 'float'
>>>
``````

We wouldn’t use `str` type in a math expression. `weight` is a number when we pass it into the function. It should remain a number.

That is on top of the error given above, `return outside function`.

1 Like

Why doesn’t this on the final slide of control flow print.

print(“I have information for the following planets:\n”)

print(" 1. Venus 2. Mars 3. Jupiter")
print(" 4. Saturn 5. Uranus 6. Neptune\n")

weight = 185
planet = 3

# Write an if statement below:

if planet == 1:
weight = weight * 0.91
elif planet == 2:
weight = weight * 0.38
elif planet == 3:
weight = weight * 2.34
elif planet == 4:
weight = weight * 1.06
elif planet == 5:
weight = weight * 0.92
elif planet == 6:
weight = weight * 1.19