Help me with my first program

This is the place for queries that don't fit in any of the other categories.

Help me with my first program

Postby smejo » Sun Mar 10, 2013 4:27 pm


I have an error with my first program and i dont know how to fix it, can someone help me? I just trying to learn code few days ago.

Here is my code (Python 2.7):
Code: Select all
def programx():
    print "Vitam ta v programe na vypocet spotreby zariadenia tarifi DD4 na Stredmon Slovensku!"
    print " "
    def watscheck():
        wats1 = raw_input("Napis vykon zariadenia vo Wattoch:")
        if wats1.isdigit() == True:
            wats = int(wats1)
            cena = wats * 0.1782490
            def cascheck():
                cas1 = raw_input("Napis cas v hodinach ako dlho je zariadenie pustene:")
                if cas1.isdigit() == True:
                    kokot = int(cas1)

                    print ("Napis cas v hodinach ako dlho je zariadenie spustene v cislach!")
            print "Napiste vykon zariadenia vo wattoch v jednom cisle!"
    def calculation():
        final = kokot * cena
        print final

It getting me error:
Code: Select all
NameError: global name 'kokot' is not defined

And i dont know what is wrong when variable wats or cena is working.

Thanks for help.
Posts: 1
Joined: Sun Mar 10, 2013 4:22 pm

Re: Help me with my first program

Postby rrashkin » Sun Mar 10, 2013 6:15 pm

The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.

Names listed in a global statement must not be used in the same code block textually preceding that global statement.

Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, or import statement.

So you would put "global kokot" at the "programx()" level.

That said, I avoid using globals as much as possible. Rather, I pass variables into functions as arguments explicitly (def functionname(variable), and othervariable=functionname(vaiable)), and return variables (in this case, kokot) from the function. So I would add "return kokot" to the end of "watscheck()" and replace the calling statement, "watscheck()" with "kokot=watscheck()". Then I would pass kokot into "calculation()": def calculation(kokot), and call it: calculation(kokot).
Bob Rashkin
User avatar
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 10 guests