FAQ: Control Flow - Review

Thank you much!!! :grinning: :smiley:

1 Like
  elif gpa>=3 and ps_score>=90 and not ec_count>=3:
    return 'This applicant should be given an in-person interview.'

this statement according to the exercise would consider ec_count>=3 as a required condition, how do I create a statement that ignores ec_count?

I’m stuck on this. I defined the function as follows:

def applicant_selector(gpa, ps_score, ec_count):

if (gpa >= 3.0) and (ps_score >= 90) and (ec_count >= 3):
return “This applicant should be accepted.”

elif (gpa >= 3.0) and (ps_score >= 90) and (ec_count < 3):
return “This applicant should be given an in-person interview.”

else:
return “This applicant should be rejected.”

choose_applicant = applicant_selector(gpa, ps_score, ec_count)

return choose_applicant

But I am given the following error:

NameError: name ‘choose_applicant’ is not defined

I don’t understand - I have defined the variable ‘choose_applicant’, so why am I getting this NameError message?

Could you please format your code as it’s hard to interpret Python code without the correction indentation. This guide covers how to do so on the forums- How do I format code in my posts?

Your syntax seems a little off. Either you have two returns in the same code block or you have a return in an unexpected location. Double check how this is written as it seems likely your error is caused by that issue.

Hi. Thanks for your help. Sorry - this is my first time posting on this forum! I’ll repeat with the code formatted.

I’m stuck on this. I defined the function as follows:

def applicant_selector(gpa, ps_score, ec_count):

if (gpa >= 3.0) and (ps_score >= 90) and (ec_count >= 3):
return “This applicant should be accepted.”

elif (gpa >= 3.0) and (ps_score >= 90) and (ec_count < 3):
return “This applicant should be given an in-person interview.”

else:
return “This applicant should be rejected.”

choose_applicant = applicant_selector(gpa, ps_score, ec_count)

return choose_applicant

But then I am given the following error:

NameError: name ‘choose_applicant’ is not defined

I don’t understand - I have defined the variable ‘choose_applicant’, so why am I getting this NameError message?

1 Like

Your code is still missing indentation which is how code is grouped in Python so it’s not entirely clear how these statements are ordered.

What is the purpose of your final line at present? Is that return inside the function definition?

I was caught out here by paretheses of all things! My code was as follows:

def applicant_selector (gpa, ps_score, ec_count):
if (gpa >= 3.0) and (ps_score >= 90) and
(ec_count >=3):
#print(return “This applicant should be accepted.”)
return “This applicant should be accepted.”

I included paretheses because on the initial example of grad reqs for this section they had a solution of:

def grad_reqs(gpa, credits):
if (credits >=120) and (gpa >=2.0):
return "You meet grad reqs)

So what I don’t understand is why following my notes and the example and including paretheses triggered an error with this project? Can anyone point me in the right direction please?

We should not need any parens on the expressions since relational operators take precedence to logical operators.

https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html

The higher up in the list, the greater the precedence. Brackets are used for grouping when we want the order of operations to follow our plan, not the default precedence. They aren’t hurting anything by being there, but the solution in the lesson checker may not have included them as they are not needed.

1 Like

Thank you for your answer that is really helpful. I think maybe the exercises have been written by different people with different preferences and the solution checker may just reflect preference (that or I missed the point of them in this exercise):

I did not include brackets and it passed but when i checked the solution as a comparison it had brackets as shown below for one of the early challenges:

Create a function called same_name() that has two parameters, your_name and my_name. IF our names are identical return True else return False

#solution given by CA

def same_name (your_name, my_name):
if (your_name == my_name):
return True
else:
return False

1 Like

Hi,
I’ve just finished this exercise and I found 2 solution for it.
I’m interested in your opinion which one of them is the better approach and why?
Also if I did miss something that could make it better.

v1

def applicant_selector(gpa, ps_score, ec_count):
if (gpa >= 3.0) and (ps_score >= 90) and (ec_count >= 3):
return “This applicant should be accepted.”
elif (gpa >= 3.0) and (ps_score >= 90) and (ec_count < 3):
return “This applicant should be given an in-person interview.”
else:
return “This applicant should be rejected.”

v2

def applicant_selector(gpa, ps_score, ec_count):
if gpa >= 3.0:
if ps_score >= 90:
if ec_count >= 3:
return “This applicant should be accepted.”
else:
return “This applicant should be given an in-person interview.”
else:
return “This applicant should be rejected.”
else:
return “This applicant should be rejected.”

Can someone please tell me what is wrong with this code!
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.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("Your weight:", weight)

I keep getting this error:
File “space.py”, line 12
elif planet == 2:
^
SyntaxError: invalid syntax

how did you get the numbers to be able to multiply to the weights after setting it equal to weight? Im not sure where those numbers came from.

1 Like

Hey!
Could be wrong here, but I believe its looking for the finale “else” statement to close off the “elif”
image

Guys, I am having some trouble with the optional space.py program. Just like @sarahpinheiro9532069
Can someone explain where I can find the relative gravity numbers? As the numbers on the full solution code do not match up with the table of conversion. Am I missing something? Please help! :blush:

I think the instructions have the correct relative values whereas the hint(s) have an incorrect list (or at least it’s not what you’d expect it to be). I’ve not seen the solution (this lesson seems to have been updated semi-recently) but you may be right that’s odd as well.

Exactly which value you need to pass the lesson I’m afraid I don’t know but the issue has been reported to cc.

The rounding isn’t the same but the following link has a suitable set of values-

There are some issues with exactly how you define values like this, e.g. exactly what is surface gravity for a big ball of gas (there is a typical convention), but it’d be helpful if there weren’t two sets of contradicting values. Hopefully it’ll be updated in due course. For now stick with whatever gets through the lesson :grinning:.

The numbers on the hint list have now been updated. Thank you for your help! :slightly_smiling_face:

I understand all the theory of this lesson and i found it easy to do the excercice with the grades, but i just dont understand what we do here, why planet 3 and so. What are we doing in this excercise. Thanks in advance

Write a space.py program that helps him keep track of his target weight by:

    Checks which number planet is equal to.
    It should then compute his weight on the destination planet.

In the given table we see that each planet is assigned a unique number, much like an ID, that can act as an index for quick look up. If we know the number we know the line it is on, rather than scanning down the list of names. Indices are very useful and as it were, very common in almost everything we do when working with serialized data.

This isn’t to suggest there is anything optimal about the given data table, just useful. Bottom line, indices are fundamental, as are tables. The exercise is only an introduction to a basic use case of them. We have one objective ahead of us… Learn to work with tables. Learn to work with indices. That is on us.

2 Likes

print(“Thank you, MTF. Now i understand it”)

2 Likes

Hi, how can I add the option, that if the entery is incorrect (not planet nr 1,2,3,4,5 or 6) that it prints Your weight: we cannot calculate your weight, please enter a correct planet nr.

This was the assignment:
Optional: Little Codey is an interplanetary space boxer, who is trying to win championship belts for various weight categories on other planets within the solar system.

Write a space.py program that helps him keep track of his target weight by:

  1. Checks which number planet is equal to.
  2. It should then compute his weight on the destination planet.

Here is the table of conversion:

# Planet Relative Gravity
1 Venus 0.91
2 Mars 0.38
3 Jupiter 2.34
4 Saturn 1.06
5 Uranus 0.92
6 Neptune 1.19
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 = 9 # 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 else: planet >= 7: weight = "we cannot calculate your weight, please enter a correct planet nr." print("Your weight:", weight)