|
why doesn't this work? thanks!
|
|
Because a might be bigger then b but not c, and the way this is written once that line of code is run the statement returns a as being the biggest, this would work in about 66% of all combinations entered but not in an instance of b < a < c. |
def biggest(a,b,c):
if a > b:
return a:
if a > c:
return a
if b > a:
return b:
if b > c:
return b
if c > a:
return c:
if c > b:
return c
print biggest (3,5,8)
In this case, let's look at the first if : if you have biggest(3,2,1), your method will return a (3) because it doesn't bother looking at c (1). |
|
Could you try to use the code formatting so that we can see how your code is indented? That can make a lot of difference. Cheers |
|
Or you can use an extra function : def biggest(a,b,c):
return bigger(bigger(a,b),c)
def bigger(a,b):
if a>b:
return a
return b
|
|
For me a simple way was
So, a gets the largest number in the end, whatever the initial numbers are...nice and easy... |
|
Well,
This won't work because you're returning Finally, if all of |
|
So, since my answer was mildly irrelevant to the question, let us check what went wrong with your reasoning: Obviously, you thought that adding a ':' after the return, should allow you to open up another level of indentation, evaluating the next level of the tree...unfortunately, it is not so: The colon, is used to define blocks, in a similar way to the BEGIN/ END or {/} of other languages. However, a block is not relevant with the return statement, since the function of the return statement is to return a value and exit the procedure. Contrary to while, if, for, function definitions and such, return is not used to begin a code block. Additionally, the colon by itself is not enough to define the start of the block,i.e. you cannot use a colon to any statement you like, in order to begin a block. That is the exact reason why your (syntactically corrected) code, corresponds to the one written by jesyspa. The syntax errors are merely the symptoms of a misunderstanding in how code works |