6 Replies - 413 Views - Last Post: 23 September 2011 - 06:30 PM Rate Topic: -----

#1 unit4216  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-July 11

authorization feature for time log program

Posted 23 September 2011 - 05:13 PM

Basically, I'm writing a program to log people clocking in and when they clock in.

It's not very sophisticated or anything, but I'm trying to add an authorization feature.

I'm basically just using a .txt file to store passwords and their corresponding names, but I'm having trouble with an error I keep getting telling me that a variable is not defined. Here's the relevant code:

 global name
    for line in "logins.txt":
        if password in line:
            loginLine = line.split('\t\t')
            name = loginLine[2]


Anyway, it keeps saying that name is not defined, even though I tried a bunch of different ways to make it global. Does anyone know how to make a variable like that global? Also, because the code I used to find the password/name combo in the .txt file is somewhat bad, if you think of a better way to do it (that involves a text file that can be manipulated (theoretically) by an office manager or seomthing) then please, let me know.

If I left out anything important, let me know.

Also, I'm very much a beginner at python, so don't ridicule me too much.

Thanks a bunch,

Pablo

Is This A Good Question/Topic? 0
  • +

Replies To: authorization feature for time log program

#2 unit4216  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-July 11

Re: authorization feature for time log program

Posted 23 September 2011 - 05:35 PM

Sorry, I forgot to add the error message, as I've noticed it's different from the others I've seen.

Also, sorry, I don't know how to edit posts.

But anyway, the specific error is this: NameError: global name 'name' is not defined

It seems to have the word "name" in it that is aside from the variable.

Hopefully that helps.
Was This Post Helpful? 0
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: authorization feature for time log program

Posted 23 September 2011 - 06:01 PM

Kind of the right idea, you don't need to register the variable as global, rather reference it when you want to access it. I'll do some examples and hopefully you can see:

name = "Alan"

def myName():
    name = "Peter"

>>> name
"Alan"
>>> myName()
>>> name
"Alan"



As you can see, the function did not change the global variable. If I do:

name = "Alan"

def myName():
   global name
   name = "Peter"

>>> name
"Alan"
>>> myName()
>>> name
"Peter"



As you can see, the function the second time modified the global variable because I told it to reference it within the function, otherwise the function's "name" variable is lost when it leaves that function's scope. Hope that helps :)

This post has been edited by Simown: 23 September 2011 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#4 unit4216  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-July 11

Re: authorization feature for time log program

Posted 23 September 2011 - 06:08 PM

View PostSimown, on 23 September 2011 - 06:01 PM, said:

Kind of the right idea, you don't need to register the variable as global, rather reference it when you want to access it. I'll do some examples and hopefully you can see:

name = "Alan"

def myName():
    name = "Peter"

>>> name
"Alan"
>>> myName()
>>> name
"Alan"



As you can see, the function did not change the global variable. If I do:

name = "Alan"

def myName():
   global name
   name = "Peter"

>>> name
"Alan"
>>> myName()
>>> name
"Peter"



As you can see, the function the second time modified the global variable because I told it to reference it within the function, otherwise the function's "name" variable is lost when it leaves that function's scope. Hope that helps :)


Thanks, I think I get it. Though, in my situation, I don't quite know if I can-- well... How would I reference it, though, the declaration is inside a for loop which is inside the function where the variable is needed later.

Basically:

function()
     for .....
          var = 9001
     otherVar = var + 5


ERROR: var is not assigned a value before it is referenced.

Quite simply, I understand the concept of global and local variables (I think), but in this case, how could I reference it if it's within the same function? It's just in a loop.
Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: authorization feature for time log program

Posted 23 September 2011 - 06:19 PM

You are trying to access the variable before it's initalised, try setting a default value, 0 maybe?

def func():
  var = 0
  for i in range(10):
        var = var + 1
  print var

>> func()
10



Of course you can assign the function to a value (if you swap return for print) to use later. Or pass the variable as a parameter to the function.

>>> myVar = func()
>>> myVar + 50
60

This post has been edited by Simown: 23 September 2011 - 06:22 PM

Was This Post Helpful? 2
  • +
  • -

#6 unit4216  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-July 11

Re: authorization feature for time log program

Posted 23 September 2011 - 06:25 PM

View PostSimown, on 23 September 2011 - 06:19 PM, said:

You are trying to access the variable before it's initalised, try setting a default value, 0 maybe?

def func():
  var = 0
  for i in range(10):
        var = var + 1
  print var

>> func()
10



You sir, are a god.

Thank you a ton, I can't believe I didn't think of this, I must be retarded.

Anyway, I will daily bow down and worship your avatar for giving me the kicking myself that I probably deserved.

Thank you,

Pablo
Was This Post Helpful? 0
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: authorization feature for time log program

Posted 23 September 2011 - 06:30 PM

Everyone makes the simplest of mistakes sometimes, don't be so hard on yourself. And it wasn't a kicking, it was some friendly advice.

Surely you won't forget next time :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1