[Tutor] Beginner - Clarifying 'understanding randint arguments'

Marc Eymard marc_eymard at hotmail.com
Mon Feb 17 12:39:29 CET 2014


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-owner at python.org
> To: marc_eymard at 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_eymard at hotmail.com
To: tutor at 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
 		 	   		  
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140217/29559b7d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: machine_guess_number.py
Type: text/x-script.phyton
Size: 1378 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20140217/29559b7d/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: shell_output.py
Type: text/x-script.phyton
Size: 1423 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20140217/29559b7d/attachment-0001.bin>


More information about the Tutor mailing list