#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    I have a doubt in Python. Help please!


    So, here’s my code:
    ‘from random import randrange
    Random1 = randrange(0,100)
    Random2 = randrange(0,100)
    print Random1
    print Random2’
    Now, when I use this code in a new window and then run it over and over again, the value of ‘Random1’ and ‘Random 2’ changes, but when I use it in interactive prompt, the value remains the same. Why is that?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    Because in interactive mode, you call once the randrange function for each of the variables Random1 and Random2, after that they hold their values during the session until you call again the randrange() function.

    Code:
    >>> from random import randrange
    >>> Random1 = randrange(0,100)
    >>> Random2 = randrange(0,100)
    >>> print Random1
    75
    >>> print Random2
    24
    >>> 
    >>> Random1
    75
    >>> Random1
    75
    >>> Random1
    75
    >>> # So now I call again the randrange() function for Random1
    >>> Random1 = randrange(0,100)
    >>> Random1
    64
    >>>
    Now when you put the code in a script (say myscript.py) and you run it as a script (non interactive mode) each time you call the function and obviously maybe different values. Here is what I get when I run the script several times in the terminal

    Code:
    $ python -tt myscript.py
    37
    13
    $ python -tt myscript.py
    9
    15
    $ python -tt myscript.py
    59
    29
    $ python -tt myscript.py
    71
    50
    $
    Regards,
    Dariyoosh
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by dariyoosh
    Because in interactive mode, you call once the randrange function for each of the variables Random1 and Random2, after that they hold their values during the session until you call again the randrange() function.

    Code:
    >>> from random import randrange
    >>> Random1 = randrange(0,100)
    >>> Random2 = randrange(0,100)
    >>> print Random1
    75
    >>> print Random2
    24
    >>> 
    >>> Random1
    75
    >>> Random1
    75
    >>> Random1
    75
    >>> # So now I call again the randrange() function for Random1
    >>> Random1 = randrange(0,100)
    >>> Random1
    64
    >>>
    Now when you put the code in a script (say myscript.py) and you run it as a script (non interactive mode) each time you call the function and obviously maybe different values. Here is what I get when I run the script several times in the terminal

    Code:
    $ python -tt myscript.py
    37
    13
    $ python -tt myscript.py
    9
    15
    $ python -tt myscript.py
    59
    29
    $ python -tt myscript.py
    71
    50
    $
    Regards,
    Dariyoosh
    OK, thanks

IMN logo majestic logo threadwatch logo seochat tools logo