The answer to exercise 1 in “10. Else Statments”, goes like this:
def graduation_reqs(gpa, credits):
if (gpa >= 2.0) and (credits >= 120):
return "You meet the requirements to graduate!"
if (gpa >= 2.0) and not (credits >= 120):
return "You do not have enough credits to graduate."
if not (gpa >= 2.0) and (credits >= 120):
return "Your GPA is not high enough to graduate."
else:
return "You do not meet the GPA or the credit requirement for graduation."
I dotn’t unserdant to which “if” is this “else” at the end related. If I’m saying that the person does not meet the gpa OR the credtis, why I should use else after the “if” that checks “not gpa > 2.0 and credits >= 120”? It shouldn’t be more logical to use it after “if (gpa >= 2.0) and (credits >= 120)”? Since I know that, if this last statment is false, then for sure GPA or credtis requirements are not being met.
The else statement is invariably connected the immediately preceding if statement; in this case: if not (gpa >= 2.0) and (credits >= 120):. The series of if statements is to ensure the returned string is the most useful information possible. Perhaps it’d read a little better with a chain of if / elif style statements but I’d not worry about it too much for this lesson. You’re looking to learn control flow for now, a lot of the coding will sometimes seem clunky and odd to try and keep it limited to concepts already introduced.
It’s fair to say the language used in that string is perhaps a little ambiguous (or at least for those of us who aren’t grammar experts such as myself) but for me personally that reads as an inclusive or in English (“you do not meet the GPA and you do not meet the credit requirement”). Potentially rephrased with a little less ambiguity as: “You meet neither the required GPA nor the credit requirements for graduation”. It may read that way entirely due to the context in which it is placed though so I’d always advocate for less ambiguity.
Were it placed immediately following the first if statement then that particular string wouldn’t quite make sense in my head: “You do not meet the GPA or the credit requirement for graduation.”. And if it were an exclusive statement I’d personally rephrase it as: “You do not meet the GPA requirements or you do not meet the credit requirements”, which seems less ambiguous to me.
That’s at least how it read to me and why using the return only when the other possibilities had been exhausted made more sense.
Since the requirements state the exact string “If a student is failing to meet both graduation requirements” then to return that string then that’s what you’ll need to do regardless of the contents of the string. Just act like it’s a client’s request and get it done. When it comes to sections where you can do your own projects and make your own choices then you can make it nice and clear and wholly unambiguous.
I spent a lot of time trying to figure out the difference between these two programs:
# first program with return
def graduation_reqs(gpa, credits):
if (gpa >= 2.0) and (credits >= 120):
return "You meet the requirements to graduate!"
if (gpa >= 2.0) and not (credits >= 120):
return "You do not have enough credits to graduate."
if not (gpa >= 2.0) and (credits >= 120):
return "Your GPA is not high enough to graduate."
else:
return "You do not meet the GPA or the credit requirement for graduation."
print(graduation_reqs(2.0,120))
#the Output of this program is:
#You meet the requirements to graduate!
# second program with print
def graduation_reqs(gpa, credits):
if (gpa >= 2.0) and (credits >= 120):
print("You meet the requirements to graduate!")
if (gpa >= 2.0) and not (credits >= 120):
print("You do not have enough credits to graduate.")
if not (gpa >= 2.0) and (credits >= 120):
print("Your GPA is not high enough to graduate.")
else:
print("You do not meet the GPA or the credit requirement for graduation.")
graduation_reqs(2.0,120)
#the Output of this program is:
#You meet the requirements to graduate!
#You do not meet the GPA or the credit requirement for graduation.
I do not understand, why the first program does correctly print only the first if statement and in the second program it prints the first if and the else statement. Can someone explain this to me?
The real difference here is the use of return instead of print. Using return stops further execution of the function and returns the object the caller (in this case you’re returning a string which is used as an argument for print). On the other hand print just sends output to a stream (typically stdout), it doesn’t end the function.
I’m noticing inconsistencies in this course’s exercises. Already commented on a few issues but seeing inconsistencies between the exercises and solutions, another issue from the else if exercise…
The instructions…
Calvin Coolidge’s Cool College* has another request for you. They want you to add an additional check to the graduation_reqs function. If a student is failing to meet both graduation requirements, they want the function to return:
“You do not meet the GPA or the credit requirement for graduation.”
But then the solution includes an if statement that prints…
“You do not meet the requirements to graduate.”
These two outputs are not logically equivalent (the first explicitly requires both to not be met while the second could include one or the other requirements not being met). Am I missing something here?
This exercise description is simply wrong. It says "If a student is failing to meet both graduation requirements, they want it to print: “You do not meet the requirements to graduate.” ".
However, adding an else statement after the if statement is not going to check this condition. The else statement is going to run even if one of graduation requirements are true which is not what we want. We only want the else statement to run if both graduation requirements are failed/false. So this exercise is simply bad logic. Please fix.
From what I can see all the alternatives are tested before the else statement and return is used within the body of each clause. Therefore the only way for this else statement to execute is if both GPA and credit requirements are too low.
A better choice might adding elif clauses so that it’s all contained in a single if statement making it more obvious that only one suite of code would ever be run (but I’m not sure elif has been introduced yet, if not it’ll pop up soon enough).
You could try a series of test on the code if you like-
I’ve not tried resetting the workspace so I’d suggest altering your own code so that it does meet the requirements. From the examples above there should be three if statements that are already part of the code, you’re just adding an else clause to the last of these statements.
Hi everybody, I need a little help on the else statements exercise.
I got confused here, I don’t know if I’m the one who misunderstood the instructions, but according to me, the code solution in the exercise prints out “You do not meet the requirements to graduate.” because the other part of the boolean expression:" gpa>=2.0" was not met, which does not answer the exercise. From what I understood we want a code that will check that both requirements are met and print the else part if otherwise.
Below is the presented code solution:
if (credits >= 120) and (gpa >= 2.0):
print("You meet the requirements to graduate!")
else:
print("You do not meet the requirements to graduate.")
Why is the indentation and capitalization so important? I’ve returned errors simply by following the default formatting when pressing enter to move to a new line only to find the indentation was off. Furthermore, isn’t the content inside a string “” practically freeform? Is that insistence on Codecademy’s part or Python in general?
I don’t see why print(“You do not have enough credits to graduate!”) should return an error compared to print(“You do not have enough credits to graduate.”)
To be honest, I have also encountered my fair share of “HUH?” when reading the instructions in some of these exercises. At first, I think it’s me not understanding Python, then I realize it’s just Codecademy with dumb logic in the descriptions.
One thing I found interesting is the indentation of the else statement. My first thought was that it needed to be indented at the same level of the print statement that follows the if statement to be part of the focus of the if evaluation (as if it were a function), meaning
if (credits >= 120) and (gpa >= 2.0):
print("You meet the requirements to graduate!")
else:
print("You do not meet the requirements to graduate.")
But that doesn’t work, and it needs to be at the same level than the if statement:
if (credits >= 120) and (gpa >= 2.0):
print("You meet the requirements to graduate!")
else:
print("You do not meet the requirements to graduate.")
Indentation is how Python groups statements together. The basic idea is that if checks if a statement is True; if it is then the suite of code (the group of statements) following it are all executed.
For an example there are three separate statements in the following if statement.
if x == 3:
x += 1 # add one to x
x * x # square it
print(x) # print text representation of it
When then expression is True we want all of these three statements to execute. Equally if it was False we want none of those statements to execute. Using the indent is how you make it clear to the Python interpreter that these statements are grouped together. They should either all be executed or none of them should be executed.
When the next statement is made without an indent that makes it clear that the grouping has ended and this new statement is part of the previous code block, not part of the group.
Since the grouping is controlled by the indent having else inside the group of statements means that it would only be used when if was True which no longer makes sense.
This grouping is what you should focus on. Other statements are also grouped this way (the body of a function, the body of a loop etc.), the statements inside the indent are grouped together and executed together, once you remove the indent you make it clear that the group has finished.