unit 2 question 21

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%20boston's gravatar image

debbie boston
1148
accept rate: 0%

edited 01 Mar '12, 08:24

fnenu-1's gravatar image

fnenu-1 ♦♦
18.5k1981231


10 Answers:

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.

link

answered 01 Mar '12, 08:14

Aaron%20McCray's gravatar image

Aaron McCray
401211

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.

link

answered 01 Mar '12, 08:20

Tharshan's gravatar image

Tharshan
843423

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 ydnayabe-1's gravatar image
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.

link

answered 01 Mar '12, 08:29

Beno%C3%AEt%20Destrub%C3%A9's gravatar image

Benoît Destrubé
2.6k21555

edited 01 Mar '12, 08:31

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

link

answered 01 Mar '12, 08:17

Beno%C3%AEt%20Destrub%C3%A9's gravatar image

Benoît Destrubé
2.6k21555

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.

link

answered 01 Mar '12, 08:19

dreyescat's gravatar image

dreyescat
6.9k163482

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

link

answered 01 Mar '12, 08:20

Ken%20Boggs's gravatar image

Ken Boggs
5.6k538

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
link

answered 01 Mar '12, 08:22

Beno%C3%AEt%20Destrub%C3%A9's gravatar image

Benoît Destrubé
2.6k21555

For me a simple way was

def biggest(a,b,c):
   if a<b:
      a=b
   if a<c:
      a=c
   return a

So, a gets the largest number in the end, whatever the initial numbers are...nice and easy...

link

answered 03 Mar '12, 13:18

Orestis%20Gklavas-4's gravatar image

Orestis Gkla...
182

edited 03 Mar '12, 13:27

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.

link

answered 03 Mar '12, 13:25

Anton%20Golov's gravatar image

Anton Golov ♦
13.3k2174174

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

link

answered 03 Mar '12, 13:50

Orestis%20Gklavas-4's gravatar image

Orestis Gkla...
182

Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • 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