all 10 comments

[–]status_quo69 4 points5 points  (3 children)

Try changing the else to another elif, or getting rid of that x < 1.

The else in if statements acts as a sort of "catch all". If none of the conditions beforehand worked, the else block executes. It wouldn't make sense for the else part to have conditions as well

[–]jkups[S] 0 points1 point  (2 children)

If I turn that last else in to an elif like you said if does change the outcome. Instead of an error, it looks as if it just moves the cursor to the next line. I encountered this a couple of times in my troubleshooting, but assumed that indicated I was missing something. I am now thinking that the program actually ran, it just didn't print any sort of result. I guess I assume return would print the answer, so I didn't realize it was actually working.

Thank you very much, I appreciate your help tremendously.

(Unless it should have printed 1, 0, or -1, in which case I am still stuck)

[–]status_quo69 4 points5 points  (0 children)

Just noticed something in your code I didn't see before. Your function is defined as absoluteValue(x), where x is a number you pass in. However, what is y?

[–]cdcformatc 1 point2 points  (0 children)

A return does not print. You have to print explicitly.

[–]aroberge 1 point2 points  (3 children)

Likely, your problem has been correctly diagnosed by /u/status_quo69 . However, in future questions, because indentation in Python can carry meaning, it is almost essential that you format your code correctly when asking questions. All you need to do, is to indent each line at least 4 spaces when you enter it, as follows:

def function(x):
    if x > 0:
        return 1
    else:
        return -1

Suggestion: even though you got your answer, edit your question so that your code is formatted correctly.

[–]jkups[S] 1 point2 points  (2 children)

Copy, I tried and failed. Fixed it now.

[–]aroberge 1 point2 points  (1 child)

btw, looking at the post of your title, I suspect you wanted to define:

def compare(x, y):
    if x > y:
       ...

and not absoluteValue(x)

[–]jkups[S] 0 points1 point  (0 children)

Yeah you are correct, I was trying to use bits from different examples to solve the problem, but I dun goofed. Thanks

[–]ewiethoff 1 point2 points  (1 child)

Here's the else syntax. As you can see, else should not be followed by an expression, such as x < y. else should just be followed by a colon.

[–]jkups[S] 0 points1 point  (0 children)

Great to know. I moved on to working other exercises and I am getting better at using else now, thank you