[Tutor] Assigning range

Steven D'Aprano steve at pearwood.info
Thu Jul 28 02:40:13 CEST 2011


Alexander Quest wrote:
> Does anyone know how to assign a certain numerical range to a variable, and
> then choose the number that is the middle of that range? For example, I want
> to assign the variable "X" a range between 1 and 50, and then I want to have
> the middle of that range (25) return with some command when I call it
> (perhaps rangemid or something like that?). In pseudocode, I am trying to
> say X = range [1,50], return middle of range (which should return 25) but I
> don't know how to code it. This is for a basic program I'm trying to write
> where the player thinks of a number and the computer tries to guess the
> number in as few tries as possible. Thanks for any help!


Forget about using range, that just adds meaningless complexity.

What is important is that you have a lower bound, and a higher bound: 
two numbers, instead of how ever many (possible thousands, or millions!) 
in range(low, high).

middle = (low+high)//2



-- 
Steven



More information about the Tutor mailing list