Skip to Content
Learn
Control Flow
Relational Operators II

Now that we’ve added conditional statements to our toolkit for building control flow, let’s explore more ways to create boolean expressions. So far we know two relational operators, equals and not equals, but there are a ton (well, four) more:

  • Greater than: >
  • Less than: <
  • Greater than or equal to: >=
  • Less than or equal to: <=

Let’s say we’re running a movie streaming platform and we want to write a function that checks if our users are over 13 when showing them a PG-13 movie. We could write something like:

def age_check(age): if age >= 13: return True

This function will take the users age and compare it to the number 13. If age is greater than or equal to 13 it will return True.

Let’s try some more!

Instructions

1.

Write a function called greater_than that takes two integer inputs, x and y and returns the value that is greater. If x and y are equal, return the string

"These numbers are the same"
2.

The nearby college, Calvin Coolidge’s Cool College (or 4C, as the locals call it) requires students to earn 120 credits to graduate. Write a function called graduation_reqs that takes an input credits and checks if the student has enough credits to graduate. If they do, return the string

"You have enough credits to graduate!"
3.

Call graduation_reqs with an input of 120 credits and print the result to the terminal. Can a student with 120 credits graduate from Calvin Coolidge’s Cool College?

Folder Icon

Sign up to start coding

Already have an account?