FAQ: Control Flow - Relational Operators II

Yes dude!!! I’m also getting this same error…message :frowning:

@discobot display help
Please do solve the web page error.

Hi! To find out what I can do, say @discobot display help.

In order to graduate, a student must have at least 120 credits.

What would happen if we called graduation_reqs(120)?

Side note: discobot cannot resolve this type of inquiry.

1 Like

No kidding. What purpose would any of us serve if that were the case? Luckily we’re only helping new STEM pursuers to reach that level of ML and AI, or at least some degree of solution analysis by the bot. The SCT would be relegated, mind.


Can I get some feedback on my control flow function, I want to know if there is anyway to rewrite this in 2 if statements instead of 3.


def greater_than(x, y):
  if x == y:
    return "These numbers are the same"
  elif x > y:
    return x
  elif y > x:
    return y

Since there are only three possibilities, then one of them can be the default, thereby eliminating one of the elifs.

    if x > y: return x
    if y > x: return y
    return "These numbers are the same".    #  default

Since each branch returns to the caller, we don’t even need an elif

Extra Study (to keep under your hat for the time being)

return x if x > y else y if y > x else "These numbers are the same"

The above is a conditional expression, rather than an if statement. Only one outcome can be returned.


ahh thank you so much I was on this for like 30 minutes trying to think of how can I make it more efficient code. thank you big time!

1 Like

You’re welcome. I would advise at this time to focus less on efficiency and more on the practical value of naive code. Write what works, first, then examine it for possible ways to simplify it. It’s not about efficiency, but diversification. Build a library in your mind of the many and varied ways to perform the same action or arrive at the same outcome. That’s the real beauty of any language, Python no less.


ok definitely ill keep that in mind

1 Like

Well I see a lot of discussion here.
Cool Dude! :grinning:


I used the following code:

def graduation_reqs(credits):

if credits >= 120:

return print("You have enough credits to graduate!")


And I received the correct result.

But the hint says I should use print in the statement call instead. Is there any disadvantage to the method I used? Going forward should the print statement be used in the statement call only? I’m asking because I’d like to understand the best practices while I’m still learning.

Thank you.

1 Like

Most functions are set up to return a useful value. That makes them a lot more flexible when it comes to writing the rest of your script. If you wanted to print the output of the function then print(myfunction(arguments)) is fairly straightforward and it also allows you to use the output, e.g. value1 = myfunction(args).

Some functions do exist solely to print but it is less common. Using return on the output of print within the function is less useful as print genrally just returns None so you have unnecessary code. It’d be better to remove the return and just use print in that case. Typically though it’s best to return the useful value and then print in the outer scope, as per the following brief example-

def myfunc():
    return True

1 Like

That’s interesting. Thank you for the explanation and the example @tgrtim.

Hi! I was programming:

if x == y :
print(‘These numbers are the same’)

And it logged ‘These numbers are the same’ to the console, but the exercise insisted I made a mistake. I edited the code slightly and it worked. The only difference is that there was double quotes instead of single quotes. Are single quotes still ok to use in Python, since the outcome was exactly the same?

Yes, single and double quotes do the same thing in python, ‘test’ == “test”.

I can only assume the difference is that sometimes the background testing for the lessons is a bit lazy and looks for exact text matches in your .py file. When run there is no difference in the string created.

Look at this code i did
if credits >= 120:

print(“You have enough credits to graduate!”)

elif credits <= 120:

print(“You don’t enough credits to graduate!”)

It works its so if the dont have enough credit it says You don’t enough credits to graduate!

Welcome to the forums!

Just wanted to point out that it’s not necessary to use the less than or equal to operator in your life statement. If credits = 120, then the if statement will execute and You have enough credits to graduate! will be printed. The elif statement won’t be evaluated because the if statement was true. Therefore, the elif statement will only be evaluated when credits < 120 and should reflect that.

I have seen a lot of mentions about whether to use “print” or “return”, so far, I have not seen it yet - maybe because the curriculum was updated.

Is knowing how to use “return” covered in subsequent lessons with the current curriculum? If not, is it because “return” can be replaced by the “print” one?