Search code examples
pythoninputraw-input

Def function python: raw_input not store


I'm newbie in python. I'm study hard to know well how python work since I starting study in 2013 at college. Sorry, if little messy.

Let me showing my problem below. I have some def function looks like:

def thread_1():
                a = input('Value UTS (100) = ')
                if a > 100:
                    print line2
                    d=raw_input('Dont higher than 100. Input y to repeat : ') 
                    d='y'
                    if d=='y' :
                        thread_1()
                    return a

def thread_2():
                b = input('Value UAS (100) = ')
                if b > 100:
                    print line2
                    d=raw_input('Dont higher than 100. Input y to repeat : ') 
                    d='y'
                    if d=='y' :
                        thread_2()
                    return b
def thread_3():                         
                c = input('Value Course (100) = ')
                if c > 100:
                    print line2
                    d=raw_input('Dont higher than 100. Input y to repeat : ') 
                    d='y'
                    if d=='y' :
                        thread_3()
def thread_4():                                                          
                value_total = a*50/100+b*30/100+c*20/100

and this my expression def into program list

if p==1:
            thread_1()
            thread_2()
            thread_3()
            thread_4()

Finally, I running this program : As long as I input number is well, but in the end program showing the error code like that :

Traceback (most recent call last):   File "ganjil-genap.py", line 71, in <module>
    thread_4()   File "ganjil-genap.py", line 36, in thread_4
    value_total = a*50/100+b*30/100+c*20/100 NameError: global name 'a' is not defined

Can anyone let me know what I have done wrong?

Thanks in advance.


Solution

  • The variables a,b and c you are using on thread_1, thread_2 an thread_3 are only defined inside those functions. 'a' is only defined inside thread_1, b inside thread_2 and c inside thread_3, but theay are not global variables of the main program. The statement

    return a 
    

    returns only the value of variable a.

    you should make the vaiables global. I think it should look like this:

    a=0
    def thread_1():
       global a
       a= raW_input....
    

    this will make your a,b,c variables global.

    Then in thread_4() a,b and c should be passed as a parametres of the function.

    def thread_4(a,b,c):
    

    I think this should work.