1

I'm struggling to understand what sort of rounding needs to be implemented to produce the grade in [pset6 readability][1].

For the Harry Potter extract provided ("Harry Potter was a highly unusual boy ..."), the Coleman-Liau formula in my program returns (before rounding) 4.555714285714288. CS50's expected output from this is Grade 5 (rounding up). My code accordingly prints Grade 5.

For the Fitzgerald extract ("In my younger and more vulnerable years ..."), my program's formula produces 7.4556521739130375. CS50's expected output from this is Grade 7 (rounding down). My code prints Grade 8.

I can see how both floats could be rounded up or down depending on what sort of rounding gets applied, but I can't think of what could be done programmatically to replicate what CS50 seems to require, as the rounding seems to go in two opposing directions in similar circumstances in different examples.

I'd conclude that there is a bug further back in my code that is leading to incorrect numbers being fed into the formula, but my program passes most of the check50 tests, so this seems unlikely.

What sort of rounding technique ought I to be applying here?

(edit to remove working code per academic honesty guidelines)

4
  • Please show how you calculate your index. I suspect that your issue is not actually anything to do with round. Also, it might be helpful to show how declared your variables that are used in your calculation. Commented Jul 13, 2021 at 20:09
  • @Robert Thanks. I've appended my code to the question. Commented Jul 13, 2021 at 20:19
  • @Robert By way of an update, I have spotted a bug how I was counting letters (the range was stopping short of "z"/"Z"). This has been fixed in the code I've included in the question but I am still not getting the correct grade for "In my younger and more vulnerable years ..." Commented Jul 13, 2021 at 22:19
  • I've now removed some redundant bits of the question too. Commented Jul 13, 2021 at 22:43

1 Answer 1

1

If you simply use round it will round the expected way (up if it's >= 0.5, down otherwise.

Python 3.9.1 (default, Jun 15 2021, 23:45:14) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 4.55
>>> b = 5.45
>>> round(a)
5
>>> round(b)
5
1
  • Thanks @curiouskiwi ! When I just use round, it now returns expected results. My bug must have been elsewhere. Commented Jul 14, 2021 at 7:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .