0

I want to get the number of days from two dates; the first being stored in an array in a function then use the number of days for some arithmetics, but I get invalid syntax and NameError "not defined" elsewhere. What am I doing wrong?

def registration():
    global reg_date, names, numbers, airtime, registration_details
    reg_date = datetime.date.today()
    names = raw_input("Names ")
    numbers = raw_input("Numbers ")   
    airtime = int(raw_input("Enter Airtime: "))
registration_details = [reg_date, names, numbers, airtime] # Store Registration Details

def status():
    global registration_details
    current_date = datetime.date.today()  # the current date may be x days since registration
    reg_date = registration_details[0] # the date of registration stored in the array during registration   
    days_since_registration = current_date – reg_date
    print  days_since_registration.days
registration()
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Sem5x
  • 13
  • 3
  • 2
    i don't see a good reason for you to be using `global`s here. why not have `registration_details` be set inside `registration`, and then be returned from that function? – abcd Mar 12 '16 at 23:44
  • @dbliss ofcourse i call the registration somewhere i just didn't want to write the full program here, it is longer than those two functions... – Sem5x Mar 12 '16 at 23:53
  • Alright @dbliss thanx, i have added the call to `registration()` – Sem5x Mar 13 '16 at 00:44
  • *please see my earlier comments*. the logic of your code makes no sense. you are accessing the contents of `registration_details` before they have been defined. there is no reason to use globals here. you have made no effort to correct any of this. now, in addition to close-voting, i feel compelled to downvote. – abcd Mar 13 '16 at 01:18
  • You could post the actual output/error message here as well. The syntax error and the NameError are two different things and we don't really know what syntax error/variable Python is complaining about. I do agree with @dbliss that your use of `global`s seems unwarranted though. You could probably pass the relevant data between functions as arguments instead and thereby avoid cluttering the name-space while increasing encapsulation. – jDo Mar 13 '16 at 01:19
  • following up on @jDo's comment, if i were to run your code as posted, this line would raise an error: `registration_details = [reg_date, names, numbers, airtime]`. this is because, as i said, `reg_date` is undefined at the time this line is executed. – abcd Mar 13 '16 at 01:20
  • I guess i should have mentioned am new to stackoverflow, python & programming in general. @dbliss removing global doesn't seem to change anything, @jDo , the error am currently getting is; `days_since_registration = current_date – reg_date ^ SyntaxError: invalid syntax` With the pointer at exactly the minus sign. – Sem5x Mar 13 '16 at 02:34
  • @Sem5x No problem :) I'm new too - been here 14 days or so. I also got a syntax error at the exact same position (I saved your code in a script called nameerror_stack.py). `SyntaxError: Non-ASCII character '\xe2' in file nameerror_stack.py on line 13, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details` Without going into the details, you might be able to fix it simply by deleting the minus sign and typing it again (if my answer didn't fix that as well) – jDo Mar 13 '16 at 02:49
  • @Sem5x no shame in being new, but you should start developing skills in diagnosing problems in your own code. execute your code line by line, making sure you're getting the results you expect at each step. when you get an error, look at the traceback, and narrow the problem down to a particular line. think about what in the line could have caused the error. there's so much you can do before you dump a block of code and a vague question on SO. – abcd Mar 13 '16 at 03:00

1 Answers1

2

There are several things you could do to keep your variables and functions neatly encapsulated while avoiding NameErrors and other issues related to scope. For one, you could use function arguments rather than global variables (it's good practice to avoid globals whenever you can. More on that here). Second, you could put your functions into classes that you instantiate and access as needed. More on that here

Maybe your code should be rewritten completely but for now, here's one way of fixing your NameError while preserving most of your original code:

import datetime

def registration():
    reg_date = datetime.date.today()
    names = raw_input("Names ")
    numbers = raw_input("Numbers ")   
    airtime = int(raw_input("Enter Airtime: "))
    return [reg_date, names, numbers, airtime]


def status(reg_info_list):
    current_date = datetime.date.today()  # the current date may be x days since registration
    reg_date = reg_info_list[0] # the date of registration stored in the array during registration   
    days_since_registration = current_date - reg_date
    print  days_since_registration.days


registration_details  = registration() # Store Registration Details
status(registration_details)
jDo
  • 3,962
  • 1
  • 11
  • 30