Hi Tutor, The previous elements I sent to the mailing list were incomplete and needs no answer from Tutor.
To clarify and re-phrase my script issue: I want to code a game whereby the computer guesses either by luck or deduction a number I pick within [0, 100]. In attached machine_guess_number.py in which line 22 statement number = random.randint(low_range + 1, high_range - 1) doesn't narrow the range: - to allow number deduction... - ... avoiding ValueError: empty range for randrange() I also attached the output of the script leading to the error showing that the loop does not exit in time to avoid error by working out the answer based on a logical narrowing of the guessing range. Hope this clarifies my previous email (see further down this note). Thanks, Marc > Subject: Your message to Tutor awaits moderator approval > From: tutor-ow...@python.org > To: marc_eym...@hotmail.com > Date: Sat, 15 Feb 2014 17:26:41 +0100 > > Your mail to 'Tutor' with the subject > > Beginner - understanding randint arguments > > Is being held until the list moderator can review it for approval. > > The reason it is being held: > > Post by non-member to a members-only list > > Either the message will get posted to the list, or you will receive > notification of the moderator's decision. If you would like to cancel > this posting, please visit the following URL: > > > https://mail.python.org/mailman/confirm/tutor/8ee9ed6d473cbc6d77ddc7af36237a9cc3b1d4b3 > From: marc_eym...@hotmail.com To: tutor@python.org Subject: Beginner - understanding randint arguments Date: Sat, 15 Feb 2014 16:25:34 +0000 Hello Tutor, I need to generate a random integer between 0 and 100. The range is supposed to be adjusted by my two variables: low_range and high_range. The logic of using the variables as part of the function arguments is to manage to get a smaller range each time the function is called excluding the possible repeat of the return value of randint. Here is what happens in my script: >>> import random >>> low_range = -1 >>> high_range = 101 >>> random.randint(low_range + 1, high_range - 1) 56 >>> low_range -1 >>> high_range 101 I was rather expecting: >>> low_range 0 >>> high_range 100 Can somebody explain why both low_range and high_range are still returning their initial values ? Thanks, Marc
#Guess my Number game #The computer has to guess the player's number by either luck or deduction #number has to be within [0, 100] import random print('Pick a number between and 0 and 100...') print('...and let me try to guess in as few attempts as possible.\n') input('Press ENTER when ready to play.\n') # set variables clue = '' count_guess = 0 low_range = -1 high_range = 101 while clue != 'correct' and low_range != high_range: number = random.randint(low_range + 1, high_range - 1) count_guess += 1 print('\nIs your number', number, end= '? ') clue = input('\n\nEnter one of the following clues:\n\n' \ + '\t\thigher\n' + '\t\tlower\n' + '\t\tcorrect\n\n' + 'Type here and press ENTER: ') if clue == 'lower': high_range = number elif clue == 'higher': low_range = number elif clue == 'correct': if count_guess > 1: print('\nYou picked', number,'and it took me only', count_guess, 'attempts to find out.') else: print('\nI could read your mind at once and saw you picked', number) if low_range == high_range: print('\nYou picked', number,'and it took me only', count_guess, 'attempts to find out.')
Python 3.3.2+ (default, Oct 9 2013, 14:50:09) [GCC 4.8.1] on linux Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> Pick a number between and 0 and 100... ...and let me try to guess in as few attempts as possible. Press ENTER when ready to play. Is your number 92? Enter one of the following clues: higher lower correct Type here and press ENTER: higher Is your number 93? Enter one of the following clues: higher lower correct Type here and press ENTER: higher Is your number 97? Enter one of the following clues: higher lower correct Type here and press ENTER: lower Is your number 95? Enter one of the following clues: higher lower correct Type here and press ENTER: higher Is your number 96? Enter one of the following clues: higher lower correct Type here and press ENTER: lower Traceback (most recent call last): File "/home/marc/Ubuntu One/Python/machine_guess_number.py", line 22, in <module> number = random.randint(low_range + 1, high_range - 1) File "/usr/lib/python3.3/random.py", line 214, in randint return self.randrange(a, b+1) File "/usr/lib/python3.3/random.py", line 192, in randrange raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) ValueError: empty range for randrange() (96,96, 0) >>>
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor