Thread: Question

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

    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0

    Question


    Hello dudes. I'm not a programmer and want to learn python. I have got a question.
    This is my code:

    Code:
    def tt():
        tt1 = random.random()
        print(tt1)
    
    tt()
    
    def dd():
        print(tt1)
    
    dd()
    How could I get it works? I think tt1 only can be used into def tt() and not outside there like dd() but I need to know if there is a way to achive it.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    Code:
    import random
    
    def tt():
        tt1 = random.random()
        print(tt1)
        return tt1
    
    a = tt()
    
    def dd(a):
        print(a)
    
    dd(a)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0
    Thank you very much.

    Here you are another situation:

    Code:
    import random
    
    def tt():
        tt1 = random.random()
    
    def dd():
        print(tt1)
    
    dd()
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    You spend some time studying the python tutorial, then return.
    Code:
    $ python
    Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
    [GCC 4.8.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print(unassigned_variable)   #<<----<<----<<----<<----<<----<<----<<----<<----<<----<<----<
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'unassigned_variable' is not defined
    >>>
    Last edited by b49P23TIvg; April 13th, 2014 at 05:20 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    I think tt1 only can be used into def tt() and not outside there
    You are correct. tt1 is what's called a "local variable" and when a function finishes executing, all the local variables are destroyed.

    If you need a value that is calculated inside a function, then good programming practice demands that you return the value from the function. There is a way to do what you want in your first example, but I'm not goign to show you because it is BAD programming practice.

    For your second example, no variable is created inside a function until the function executes.

    Programmers have written complex programs like Windows, Mac OSX, and the Linux operating system, as well as games like Call of Duty without doing what you are trying to do, so ANYTHING you are trying to program can be done without your two examples.
    Last edited by 7stud; April 12th, 2014 at 11:40 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You spend some time studying the python tutorial, then return.
    I'm learning python slowly and practicing it at the same time. Every time I learn something new, my mind asks itself new questions and I try to answer them coding examples. I sometimes find the answer, sometimes not. When it's not, I look for the answer in tutorials, forums, etc. Only when I don't find it, I ask for help.


    Originally Posted by 7stud
    For your second example, no variable is created inside a function until the function executes.

    Programmers have written complex programs like Windows, Mac OSX, and the Linux operating system, as well as games like Call of Duty without doing what you are trying to do, so ANYTHING you are trying to program can be done without your two examples.
    I only want a simple task, a bucle where a random number is different than the previous one.

    Code:
    def tt():
        rVar1 = random.random()
        return rVar1
    
    rNum = tt()
    
    print(rNum)
    print(rNum)

    Any ideas?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    I only want a simple task, a bucle where a random number is different than the previous one.
    Looking up 'bucle' in a dictionary, it appears that 'bucle' is Spanish for 'loop'.

    PHP Code:
    import random

    def get_random_number
    ():
        return 
    random.random()

    for 
    i in range(10):
        print 
    get_random_number()

    --
    output:--
    0.793932650793
    0.686940509458
    0.786492909274
    0.518099925315
    0.389774651281
    0.680193877646
    0.524266765072
    0.229927802936
    0.850421474636
    0.425864983658 
    Of course, it would be much simpler to write:

    PHP Code:
    import random

    for i in range(10):
        print 
    random.random() 
    Last edited by 7stud; April 13th, 2014 at 12:27 PM.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Posts
    4
    Rep Power
    0
    Thank you 7stud. Your script works but it's not exactly what I want. I've just solve it changing 'the point of view' but I'd like to know if it's possible.

    Briefly and more accurately:

    Code:
    import random
    
    def get_random_number():
        rNum = random.random()
        return rNum
    
    random_num = get_random_number()
    
    def greeting():
        if random_num <= float(1)/3:
            print('Hello')
        elif random_num <= float(2)/3:
            print('How are you?')
        else:
            print("What's up?")
    
    def main():
        get_random_number()
        greeting()
    
    main()
    main()
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    Another way to rename library functions is

    from random import random as get_random_number


    Code:
    import random
    
    def greet():
        random_num = random.random()
        if random_num <= float(1)/3:
            print('Hello')
        elif random_num <= float(2)/3:
            print('How are you?')
        else:
            print("What's up?")
    
    def greetdwl():
        r = 3 * random.random()
        return ('Hello', 'How are you?', "What's up?",)[(1 <= r) + (2 <= r)]
    
    def greetdwl2():
        return random.choice(('Hello', 'How are you?', "What's up?",))
    
    def main():
        greet()
        print(greetdwl())
        print(greetdwl2())
    
    for a in 'several trials':
        main()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo