Homework 2.3: where is the mistake

# Define a procedure, median, that takes three
# numbers as its inputs, and outputs the median
# of the three numbers.

# Make sure your procedure has a return statement.

def bigger(a,b):
    if a > b:
        return a
    else:
        return b

def biggest(a,b,c):
    return bigger(a,bigger(b,c))

def median(a, b, c):
    h = biggest(a, b, c)
    if h==a:
        x = b
        y = c
    if h==b:
        x = a
        y = c
    else:
        x = a
        y = b
    return bigger(x, y)
print median (7,8,7)

asked 08 Mar '12, 13:07

Navneet%20Vishwakarma's gravatar image

Navneet Vish...
103
accept rate: 50%

edited 08 Mar '12, 23:22

Rob%20Barnes-3's gravatar image

Rob Barnes-3 ♦
19.1k1068205

When you post code, you want to make sure it's formatted properly so we can read it.
Please review this post if you need to know how to format your code for the forums.

(08 Mar '12, 23:22) Rob Barnes-3 ♦ Rob%20Barnes-3's gravatar image

4 Answers:

It doesn't work if the first number is the biggest.
median(3,1,2) returns 3

link

answered 08 Mar '12, 13:10

Tim%20Gl%C3%A4%C3%9Fer's gravatar image

Tim Gläßer
28527

yeah ... got it
thanks Tim

link

answered 08 Mar '12, 13:13

Navneet%20Vishwakarma's gravatar image

Navneet Vish...
103

Please, format your code with 101010 button. It will be easier to read, and easier to find what's wrong. Thanks.

link

answered 08 Mar '12, 13:14

macjohn's gravatar image

macjohn
4.3k93372

I'll remember from next time

(08 Mar '12, 23:21) Navneet Vish... Navneet%20Vishwakarma's gravatar image

I have the same issue...cant figure problem with my solution..

link

answered 08 Mar '12, 13:20

nty's gravatar image

nty
245

you have to either return after condition statement or make the second one as 'elif'

the problem is ....
even if the first condition is true, being second condition false, else statement is changing the value and producing wrong result for every test case where first condition holds true.

(08 Mar '12, 23:24) Navneet Vish... Navneet%20Vishwakarma's gravatar image
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,219
×1,718
×76
×43

Asked: 08 Mar '12, 13:07

Seen: 216 times

Last updated: 08 Mar '12, 23:24