Python control flow 12/12

I have a question about the numbers in the table of conversion in the next exercise: https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-control-flow/lessons/python-control-flow/exercises/review

I notice that the numbers for the relative gravity of the planets in the table of conversion are different from the numbers for relative gravity in the ‘hint’ section. I for example wrote down 0.91 for the relative gravity of venus, whilst in the hint section it says 0.78.

For reference, this is my solution to the exercise:

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:" + str(weight))

and in the hint section it says this:

if planet == 1:
  weight = weight * 0.78
elif planet == 2:
  weight = weight * 0.39
elif planet == 3:
  weight = weight * 2.65
elif planet == 4:
  weight = weight * 1.17
elif planet == 5:
  weight = weight * 1.05
elif planet == 6:
  weight = weight * 1.23
 
print("Your weight:", weight)

I’m not sure if i’m missing some if i’m missing something here? Thanks in advance for your help!!

PS: I also have another tiny question relating to this exercise:
Why is it necessary to say:

weight = weight * 1.23

instead of just:

weight * 1.23

Thank you very much for your patience :slight_smile:

1 Like

That seems weirdly inconsistent and out of order, I don’t think it’s you that looks like something might have been changed but one or the other section wasn’t updated. The basic instructions (not the hint) have the correct conversions from what I can tell.

As for the weight calculation what’s the different between the syntax of those two lines. Why might = be necessary? If you altered your code to use the second option what would the final output be?

2 Likes

The only thing I can think of is that by saying:

weight*1.23

instead of

weight = weight*1.23

I alter the variable itself instead of the value of the variable. But I honestly don’t see how this is different from each other?

1 Like

Multiplication of a variable (or similar mathematical operations) does not affect the variable itself. Short example-

x = 3
# evaluate the expression of (x * 2), print result
print(x * 2)  
Out: 6
# Has x changed?
print('x={}'.format(x))
Out: x=3

So with weight*1.23 this expression is evaluated, creating a new object (we might expect a float in this case) but this new object is never assigned to anything (=) so it is just set for garbage collection. The object identified by weight would be unchanged.

There’s a series of operators that act like this for example to multiply and assign *= can be used in the following way, weight *= 1.23 which is functionally equivalent to weight = weight * 1.23.

2 Likes

Hi! I’m having the same problem: I can’t figure out what is wrong with my answer. An the answer they present in the tip does not work at all. Does an error messange appears to you? If it does, what they say?

I’m really thinking this is a bug, as not even the asnwer provided by them is working, but they said me to come here to figure out my error… ;.; I’m not sure if I’m happy or sad in knowing another person is having absolutly the same problem than I .

If they resolve this problem by e-mail I’ll let you know.

Thank you very much! I get it now :wink:

1 Like

What kind of error do you get? If you put your code in the comment, maybe I can help you with it. You can use the </> (in the bar above this comment section) to enter your code.

1 Like

So, I have no ideia what kind of error I get, because no error messanger appears at all. I even tried to copy-paste the solution they presented and I’m unable to keep going with exercises - the “Up next” is not activated nor a messanger appears giving me a tip of what is wrong.

Here is my code (that, right now, is exactly what I copied-pasted from them):

`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.78
elif planet == 2:
weight = weight * 0.39
elif planet == 3:
weight = weight * 2.65
elif planet == 4:
weight = weight * 1.17
elif planet == 5:
weight = weight * 1.05
elif planet == 6:
weight = weight * 1.23

print(“Your weight:”, weight)
`

hi all, i was confused with this exercise too… i noticed that in the “table of conversion” there are 7 planets that begins with Mercury 0.38 BUT in the script and in the hint there are only 6 planets… and it begins with VENUS 0.91 , so i’m guessing lots of confusion comes form that difference…

looking ate the table first i did this>>>>

if planet == 1:

weight = weight * 0.38

elif planet == 2:

weight = weight * 0.91

elif planet == 3:

weight = weight * 0.38

elif planet == 4:

weight = weight * 2.34

elif planet == 5:

weight = weight * 1.06

elif planet == 6:

weight = weight * 0.92

elif planet == 7:

weight = weight * 1.19

print(“Seu peso é:” + " " + str(weight))

and it prints :
Seu peso é: 70.3

and it checked correct… with 7 planets

then i realized the info in the scrip was only with 6 planets and then i copied the hint code
it printed :
Your weight: 432.9

and it still checked ok for the answer

sooooo it has 2 correct answers ?

Piggybacking this thread:

Why can’t I do the below? I’m getting an SyntaxError: 'return' outside function error.

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:
  return weight * .91
elif planet == 2:
  return weight * .38
elif planet == 3:
  return weight * 2.34
elif planet == 4:
  return weight * 1.06
elif planet == 5:
  return weight * .92
elif planet == 6:
  return weight * 1.19
else:
  print("Please enter a number")

Thank you.

https://www.codecademy.com/paths/data-analyst/tracks/dacp-python-fundamentals/modules/dscp-python-control-flow/lessons/python-control-flow/exercises/review

The return statement is understood used inside functions so adding it in other locations will throw the error you see. If you meant to wrap it in a function then add that in or if not just use assignment or something for the relevant values.

I appreciate your response, but am more confused now.

Return is used in the following way-

def func():
    return something

Outside of a function it cannot be used.

1 Like

Sweet and simple. Thank you!

1 Like