I have an assignment to:
"Wrap this code in a function called compare(x, y).
Call compare three times: one each where the first
argument is less than, greater than, and equal to the second argument."

So I came up with this:

def compare(x, y):
    if x < y:
        print x, "is less than", y
    elif x > y:
        print x, "is greater than", y
    else:
        print x, "and", y, "are equal"
print compare(3, 5)
print compare(5, 3)
print compare(5, 5)

The problem is my result:

>>> 
3 is less than 5
None
5 is greater than 3
None
5 and 5 are equal
None
>>>

What's with the "None"? I don't see anything that would give that output.
On a side note, how do you get script to show in the forum with colors like IDLE?

The reason it's printing None is that you're not returning anything from the function. By using print on compare(), you're asking print to print whatever compare returns. compare returns nothing, hence it prints None.

To get syntax coloring, change your start code tag to [code=python]

That's the problem. I eliminated the print comand and simply called the function. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.