# What's wrong with this code?

**mtf**#2

The sum of two integers may possibly be odd parity, which when divided by 2 will be rounded down in Python 2 since integer division gives an integer quotient.

Try,

```
return float(inputlist[q] + inputlist[r]) / 2
```

Aside

To get a count of the number of elements in a list we have the `len()`

function.

```
n = len(inputlist)
```

By reassigning the sorted list to the same variable you are effectively changing the global object (the list outside of the function, if it exists), which is the same as,

```
inputlist.sort()
```

Safer for all practical purposes if we leave the global object unchanged, and only sort the local copy.

```
s = sorted(inputlist)
```

The midpoint in the list is the same for both odd and even parity, only the even parity has two midpoints.

```
m = int(n / 2)
```

For even parity, the midpoints are,

```
s[m - 1] and s[m]
```

There is enough information above to determine the median, without introducing a whole bunch of code.

```
if n % 2:
return s[m]
else:
return float(s[m - 1] + s[m]) / 2
```

why isn’ t it printing ‘p’ = (count + 1) / 2?

also if you see that conditon where q and r are involved , it will always give out even num.

**mtf**#4

Please explain your code for us so we can better understand the logic. Very little of it makes any sense to me.

- Why do we need to run
`sorted()`

inside the loop? - Why do we need to return count on line 10?
- Why are the print statements
*after*the return statements?

Can you prove that?

problem solved . thank you, I should’nt have returned the statement else everything else is right.

**system**#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.