Relational Operators II


#1

Hey, just wondering if someone can explain why I am not getting any output in this code.

The first exercise is:

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”

The code I created, and which also ticked the box was as follows:

def greater_than(x, y):
  if x < y:
    return y
  if x == y:
    return "These numbers are the same"
  
  greater_than(-8, -8)

However, it didn’t give me any feedback.

The second exercise is:

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!”

The code I created was as follows:

def graduation_reqs (credits):
  if credits >= 120:
    	return "You have enough credits to graduate!"
  else:
    return "go home"

graduation_reqs (100)

But again, no output.

If anyone could explain why there is no output that would be so helpful!!

Thanks in advance.

Gurpal


#2

to get output, you need print, so for example print graduation_reqs( 100 ) to print the returned result

in the first exercise, the function call (which executes the function) is nested in the function, is that what you want here?


#3

Hey

Thanks for getting back to me so quickly!!

I didn’t want the function call nested in the function, thanks pointing that out.

I was getting the output when including print in the code as opposed to return but I thought that I was also meant to get some output even by just having return in the code. I see where I went wrong now, I should have included the following code when calling the function!

print (greater_than(-8, -8))

print(graduation_reqs (120))

If return doesn’t output the message, why would I include that in my code as opposed to print? (sorry that’s probably a really noob question!).

Thanks for the help!


#4

although function calling (other) functions can be very useful:

def add_one(x):
   return x + 1

def add_two(y):
   return add_one(y) + add_one(y)

print(add_two(3))

okay, not the greatest example in the world, but its just give you an idea of how it works.

and now i can also go about explaining return based of this example. because x is a local variable of add_one function, we can’t access it outside the add_one function. Which is why we use return, so we can pass data from one function to another

each function should only do one thing, this makes your code better to test (tests are useful when your application gets bigger, to verify your code is working correctly), if its easier to write test it generally means your code quality is better/code is better structured


#5

Thanks Stetim! That really has helped me get my head around this, really appreciate it man!


#6

actually for demonstration purpose this might have been better:

def add_one(x):
   x = x + 1
   return x

def add_two(y):
   # print(x)
   y = add_one(y) + add_one(y)
   return y

# print(y)
print(add_two(3))

now you could insert print statement to trigger the errors (of variable you have no access to/outside of scope)

good to hear it helped you to wrap your head around these concepts, now you just have to practice them :slight_smile:


#7
def greater_than(x, y):
  if x < y:
    return y
  if x == y:
    return "These numbers are the same"

Not sure why this one was accepted, since it does not return x in any conditional branch.

Aside

Maybe it’s because I tend to be anal about literal expressions but it would be nice to see a > operator in use so it parallels the name given.

x > y  ?  return x

y > x  ?  return y

In an if..return scenario the equality could be the default return (no conditional).

return "These numbers are the same"

So long as the function returns the correct value all the time, it really doesn’t matter what operations we use, but as I stated, it would be nice to see the corresponding operator suggested in the name of the function. It’s got a semantic feel to it.


Extra Study

You’ll be covering logical operators very soon (in this module, I believe) so when you get a clear understanding of them by completing all the exercises, come back to this solution and study it…

>>> def greater_than(x, y):
	return x > y and x or y > x and y or "These numbers are the same"

>>> greater_than(6, 7)
7
>>> greater_than(8, 7)
8
>>> greater_than(7, 7)
'These numbers are the same'
>>> 

Definitely don’t use it in your exercise. Even though it works as expected, it’s not the correct solution as expected by the lesson checker.


#8

Thanks Stetim, that’s really helped clear up how the data from one function can be used by another function with return.

lol i’ll definitely be practicing these now! :muscle:t5:


#9

Thanks for the comment Roy. I didn’t realise until you pointed it out that I had written the code the opposite way to what the exercise was asking. It makes total sense to have coded x > y as opposed to the way I did it. I’ll keep that in mind next time I’m coding!

Appreciate the extra study points too :slight_smile: i’ll be back to wrap my head around them once I reach those concepts!


#10

yep, you can do fun things with it:

def greater_than(x, y):
    return x > y and x or y > x and y or "These numbers are the same"

a = greater_than(6, 7)
b = greater_than(8, 7)
print( greater_than( a, b ) )

or:

def greater_than(x, y):
    return x > y and x or y > x and y or "These numbers are the same"

print( greater_than( greater_than(6, 7), greater_than(8, 7) ) )

which is the same, but without the intermediate step


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.