-1

So I am trying to find the median of the list "revenues" which will be named "base_revenue".

Comments:

#Assume that revenues always has an odd number of members

#Write code to set base_revenue = midpoint of the revenue list

#Hint: Use the int and len functions

revenues = [280.00, 382.50, 500.00, 632.50, 780.00]


 {def findMiddle(revenues):
    middle = float(len(revenues))/2
if middle % 2 != 0:
    return revenues[int(middle - .5)]
else:
    return (revenues[int(middle)], revenues[int(middle-1)])}

I'm getting invalid syntax. The median function itself works, but maybe there is a more efficient way to do it.

  • Could you format your code using code fences (using triple `)? – BrokenBenchmark Mar 16 '22 at 02:52
  • 2
    Sorry, but it looks like you copied a function off SO without understanding the basic syntax of Python. Please look up how to declare and use functions in Python and try again, then come back if you're still having issues. – Loocid Mar 16 '22 at 02:55
  • 1
    `base_revenue = { def findMiddle(revenues):` What are you doing here? This syntax is all wrong. – John Gordon Mar 16 '22 at 02:57
  • 2
    "I'm getting invalid syntax" - please always post the [full traceback](//realpython.com/python-traceback) when asking about errors - they're full of information that's critical to debugging :) – Michael Delgado Mar 16 '22 at 03:09

1 Answers1

1

Hint: the answer to this is far simpler than you've made it. You can even do it in a single line, unless your instructor specifically requires you to define a function.

You're told the list will always have an odd number of items; all you need is the index of the middle item. Remember that in Python, indices start at 0. So, for instance, a list of length 5 will have its middle element at index 2. A list of length 7 will have its middle element at index 3. Notice a pattern?

Your assignment also reminds you about len(), which finds the length of something (such as a list), and int(), which turns things (if possible) into integers. Notably, it turns a floating-point number into the the closest integer at or below it (a "floor" function); for instance it turns 2.5 into 2.

Can you see how you might put those together to programmatically find the midpoint index?

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
  • 1
    I don't see a reasonable way to use `int()` for this :-) – Kelly Bundy Mar 16 '22 at 03:28
  • @KellyBundy Well it would be simpler to use `//` for integer division, but since their assignment specifically mentions `int()`, I'm guessing that their instructor is building on what the class has already gone over. It'll get you to the same place, just slightly less directly. Suboptimal doesn't necessarily mean unreasonable. – CrazyChucky Mar 16 '22 at 03:31
  • Hmm, I actually wonder now... what will humanity reach first? Memory/lists so large that `len(lst)/2` is inaccurate, or Python floats using more than 64 bits? :-) For ranges, that method already [is wrong](https://tio.run/##K6gsycjPM7YoKPr/P1HBVqEoMS89VcMCA2hyFRRl5pVoJEaDyJzUPI1ETQV9BSPNWM3//wE). – Kelly Bundy Mar 16 '22 at 03:44
  • When the optimal one is trivial and better in every way, I do think that suboptimal one is unreasonable. – Kelly Bundy Mar 16 '22 at 03:46
  • These are definitely good things to know about! I just don't think it's fundamentally wrong for instructors to teach certain parts of a field at a time. Most of us all learned about positive numbers before negative, and atoms before quarks, even though they're simplified, imperfect subsets. Perhaps the instructor is planning to mention integer division tomorrow, who knows? Errors in floating point arithmetic aren't likely to be an issue for simple, early programs with small amounts of data. – CrazyChucky Mar 16 '22 at 03:49
  • Maybe not for simple, early programs with small amounts of data, but if they fail to learn the right way, they might get into trouble due to it later on. I think I've seen stackoverflow questions about that (people using `/` instead of `//` and then acting all surprised that their numbers are wrong). – Kelly Bundy Mar 16 '22 at 04:04