# unit 2 question 21

 0 why doesn't this work? thanks! 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) asked 01 Mar '12, 08:11 debbie boston 11●4●8 accept rate: 0% fnenu-1 ♦♦ 18.5k●19●81●231

 2 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. answered 01 Mar '12, 08:14 Aaron McCray 401●2●11
 2 def biggest(a,b,c): if (a > b and a > c): return a if (b > a and b > c): return b if (c > a and c > b): return c print biggest(6,2,4)  I think what aaron said was right, you have to test the other numbers together. My code is above, have a look and try and see what the difference is between yours. answered 01 Mar '12, 08:20 Tharshan 843●4●23 This is a nice piece of code from tharshan09. This approach could be used to directly create the 'median' code with a small and clear piece of code, example line- if (a > b and b > c): return b, and so on (03 Mar '12, 13:25) ydnayabe-1
 1 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). What might be hard to understand is that calling "return" stops the processing of the function. It returns the result without looking at what was next. answered 01 Mar '12, 08:29 Benoît Destrubé 2.6k●2●15●55
 0 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 answered 01 Mar '12, 08:17 Benoît Destrubé 2.6k●2●15●55
 0 In Python indentation is vital to understand the cod. So it is hard to read how really your code looks like. Try to format it a little.  def biggest(a, b, c): if a > b: ... this is done enclosing your code between pre tags. answered 01 Mar '12, 08:19 dreyescat 6.9k●16●34●82
 0 In debugging a program, I recommend fixing the obvious first and try again. In your return expressions, no colon (:) should be present. In other words: return a: must be changed to return a answered 01 Mar '12, 08:20 Ken Boggs 5.6k●5●38
 0 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  answered 01 Mar '12, 08:22 Benoît Destrubé 2.6k●2●15●55
 0 For me a simple way was def biggest(a,b,c): if a
 0 Well, return a: is always and definitely wrong: there is simply no case in Python where a return statement may be followed by a loose colon (counterexamples welcome). If we get rid of that and remove the accompanying indentation: 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)  This won't work because you're returning a no matter whether a > c or not, as long as a > b. Same goes for a > c; in fact, the situation is even worse, as you know that a <= b, so returning a if a > c is probably going to be wrong. Finally, if all of a, b and c are equal, you will return None, which is never the correct answer. answered 03 Mar '12, 13:25 Anton Golov ♦ 13.3k●21●74●174
 0 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 answered 03 Mar '12, 13:50 Orestis Gkla... 18●2
Question text:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "Title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

### Tags:

×15,292
×369
×236
×18

Asked: 01 Mar '12, 08:11

Seen: 250 times

Last updated: 03 Mar '12, 13:50