Fixed issue #2839, raised ValueError if value >= n_values

Code newbie, only my second ever PR, so sorry if I screwed up somewhere.