0

I'm new to Python programming and I'm stuck with the following error:

form="""
    <form method="post">
    What is your birthday?
    <br>

    <label> Month
        <input type="text" name="month">
    </label

    <label> Day
        <input type="text" name="day">
    </label>

    <label> Year
        <input type="text" name="year">
    </label>

    <br>
    <br>
    <input type="submit">
</form>
"""

class MainPage(webapp2.RequestHandler):

def get(self):
        self.response.out.write(form)

def valid_day(day):
        if day and day.isdigit():
            day = int(day)
            if day > 0 and day <= 31:
                return day

def valid_year(year):
        if year and year.isdigit():
            year = int(year)
            if year > 1899 and year < 2021:
                return year
def post(self):
        user_day = valid_day(self.request.get('day'))
        user_year = valid_year(self.request.get('year'))

I'm told that global name 'valid_day' is not defined. Can anybody tell me where is the fault? I think I defined the function valid_day / valid_year above so it should be accessible, or not?

2
  • Are some of these functions in a class or something? Is the indentation you're showing us here exactly what you have in your script? Are you using python 2 or 3 (matters because the two handle mixed whitespacing differently) Commented Aug 4, 2015 at 17:16
  • Where did you define these functions? Are they in a class? As shown, if this is in the top-level of a file, the valid_day and valid_year functions are defined and can be accessed. If this is in a class, then that is different. Commented Aug 4, 2015 at 17:17

1 Answer 1

2

It appears by your use of self as a parameter to the post function that this code is inside a class.

In that case, you need to use self to access the methods. (Normally your assumption about scoping and defining functions above the call would be correct.)

Rewrite your post function as follows:

def post(self):
    user_day = self.valid_day(self.request.get('day'))
    user_year = self.valid_year(self.request.get('year'))
Sign up to request clarification or add additional context in comments.

8 Comments

And valid_day/valid_year probably want @static_method decorators in that case.
Err, yes, I was assuming they're also inside the class, in which case they should also use self as the first parameter, which they currently don't. Probably adding that is better unless there's some compelling reason they shouldn't be a part of the same class.
I changed it that way but then I get an error that my function gets 2 arguments instead of one. I edited the Code in my question.
@rikojir Yes that's due to the discussion we were just having above. You need to rewrite the definition of valid_day and valid_year to also have self as a parameter. So def valid_day(day) becomes def valid_day(self, day) and likewise for valid_year.
@rikojir Make sure you understand the basics of class design and why self is necessary in Python. This seems to be a good discussion just at a glance. (Maybe someone else can suggest something better.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.