Python 2.7 名称错误:全局名称';有效月份;没有定义

Python 2.7 名称错误:全局名称';有效月份;没有定义,python-2.7,web-deployment,web-development-server,Python 2.7,Web Deployment,Web Development Server,我正在学习Udacity的Web开发课程,在第2单元35中遇到了一个问题。这是我的密码 import webapp2 form=""" <form method="post"> What is your birthdate? <br> <label> Month <input type="text" name="month"> </label> <label> Day

我正在学习Udacity的Web开发课程,在第2单元35中遇到了一个问题。这是我的密码

import webapp2

form="""
<form method="post">
    What is your birthdate?
    <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):
    months = ['January','February','March','April','May','June','July',
          'August','September','October','November','December']
    months_abbvs = dict((m[:3].lower(), m) for m in months)


    def valid_month(month):
        if month:
            short_month = month[:3].lower()
            if short_month in months:
                return months_abbvs.get(short_month)

    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 >= 1900 and year <= 2020:
                return year

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

    def post(self):
        user_month = valid_month(self.request.get('month'))
        user_day = valid_day(self.request.get('day'))
        user_year = valid_year(self.request.get('year'))

        if not (user_month and user_day and user_day):
            self.response.out.write(form)
        else:
            self.response.write("Thanks! That's a totally calid day!")

app = webapp2.WSGIApplication([
        ('/', MainPage)], debug=True)
当我在运行服务器的地方看到cmd提示符时,我得到一个错误:

INFO     2017-11-01 22:09:22,153 module.py:821] default: "GET / HTTP/1.1" 
200 382
ERROR    2017-11-02 02:09:46,470 webapp2.py:1528] global name 
'valid_month' is not defined
Traceback (most recent call last):
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in 
__call__
rv = self.handle_exception(request, response, e)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in 
__call__
rv = self.router.dispatch(request, response)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in 
default_dispatcher
return route.handler_adapter(request, response)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in 
__call__
return handler.dispatch()
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in 
dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in 
dispatch
return method(*args, **kwargs)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\python-docs-
samples\appengine\standard\hello_world\main.py", line 50, in post
user_month = valid_month(self.request.get('month'))
NameError: global name 'valid_month' is not defined
ERROR    2017-11-02 02:09:46,470 wsgi.py:279]
Traceback (most recent call last):
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 
267, in Handle
INFO     2017-11-01 22:09:46,477 module.py:821] default: "POST / 
HTTP/1.1" 500 -
result = handler(dict(self._environ), self._StartResponse)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1519, in 
__call__
response = self._internal_error(e)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in 
__call__
rv = self.handle_exception(request, response, e)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in 
__call__
rv = self.router.dispatch(request, response)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in 
default_dispatcher
return route.handler_adapter(request, response)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in 
__call__
return handler.dispatch()
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in 
dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\google-cloud-
sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in 
dispatch
return method(*args, **kwargs)
File "C:\Users\mmelv\AppData\Local\Google\Cloud SDK\python-docs-
samples\appengine\standard\hello_world\main.py", line 50, in post
user_month = valid_month(self.request.get('month'))
NameError: global name 'valid_month' is not defined
据我所知,问题出在 用户月份=有效月份(self.request.get('month))
行,但我尝试过将缩进从空格改为制表符,再改回空格,重写整个代码,重新启动服务器,但都没有用。请有人帮帮我…

当从成员函数所属的类中调用成员函数时,您需要在它们前面加上
self.
。此外,所有成员函数,如
valid\u month()
valid\u day()
valid\u year()
必须将
self
作为其实际参数的第一个值。此外,对静态类变量的任何引用,如
months
months\u abvs
也必须以
self.
开头

此外,返回值
valid\u month()
valid\u day()
valid\u year()
中可能存在逻辑错误。每个都返回一个字符串,但是
post()
中的条件正在评估这三个是否都是
True
。此外,条件in
valid\u months()
可能用于检查
short\u month
是否在
months\u abvs
中,而不是在
months

尝试用以下内容替换当前的
post()
函数:

def post(self):

    user_month = self.valid_month(self.request.get("month"))
    user_day   = self.valid_day(self.request.get("day"))
    user_year  = self.valid_year(self.request.get("year"))

    if not(user_month and user_day and user_day):

        self.response.out.write(form)

    else:

        self.response.write("Thanks! That's a totally calid day!")
def valid_month(self, month):

    if month:

        short_month = month[:3].lower()

        if short_month in self.months_abbvs:

            return True
def valid_day(self, day):

    if day and day.isdigit():

        day = int(day)

        if day > 0 and day <= 31:

            return True
def valid_year(self, year):

    if year and year.isdigit():

        year = int(year)

        if year >= 1900 and year <= 2020:

            return True
然后更改以下函数标题:

对于
valid\u month()
,将现有函数替换为以下内容:

def post(self):

    user_month = self.valid_month(self.request.get("month"))
    user_day   = self.valid_day(self.request.get("day"))
    user_year  = self.valid_year(self.request.get("year"))

    if not(user_month and user_day and user_day):

        self.response.out.write(form)

    else:

        self.response.write("Thanks! That's a totally calid day!")
def valid_month(self, month):

    if month:

        short_month = month[:3].lower()

        if short_month in self.months_abbvs:

            return True
def valid_day(self, day):

    if day and day.isdigit():

        day = int(day)

        if day > 0 and day <= 31:

            return True
def valid_year(self, year):

    if year and year.isdigit():

        year = int(year)

        if year >= 1900 and year <= 2020:

            return True
对于
valid\u day()
,将现有函数替换为以下内容:

def post(self):

    user_month = self.valid_month(self.request.get("month"))
    user_day   = self.valid_day(self.request.get("day"))
    user_year  = self.valid_year(self.request.get("year"))

    if not(user_month and user_day and user_day):

        self.response.out.write(form)

    else:

        self.response.write("Thanks! That's a totally calid day!")
def valid_month(self, month):

    if month:

        short_month = month[:3].lower()

        if short_month in self.months_abbvs:

            return True
def valid_day(self, day):

    if day and day.isdigit():

        day = int(day)

        if day > 0 and day <= 31:

            return True
def valid_year(self, year):

    if year and year.isdigit():

        year = int(year)

        if year >= 1900 and year <= 2020:

            return True

从成员函数所属的类中调用成员函数时,需要在其前面加上
self.
。此外,所有成员函数,如
valid\u month()
valid\u day()
valid\u year()
必须将
self
作为其实际参数的第一个值。此外,对静态类变量的任何引用,如
months
months\u abvs
也必须以
self.
开头

此外,返回值
valid\u month()
valid\u day()
valid\u year()
中可能存在逻辑错误。每个都返回一个字符串,但是
post()
中的条件正在评估这三个是否都是
True
。此外,条件in
valid\u months()
可能用于检查
short\u month
是否在
months\u abvs
中,而不是在
months

尝试用以下内容替换当前的
post()
函数:

def post(self):

    user_month = self.valid_month(self.request.get("month"))
    user_day   = self.valid_day(self.request.get("day"))
    user_year  = self.valid_year(self.request.get("year"))

    if not(user_month and user_day and user_day):

        self.response.out.write(form)

    else:

        self.response.write("Thanks! That's a totally calid day!")
def valid_month(self, month):

    if month:

        short_month = month[:3].lower()

        if short_month in self.months_abbvs:

            return True
def valid_day(self, day):

    if day and day.isdigit():

        day = int(day)

        if day > 0 and day <= 31:

            return True
def valid_year(self, year):

    if year and year.isdigit():

        year = int(year)

        if year >= 1900 and year <= 2020:

            return True
然后更改以下函数标题:

对于
valid\u month()
,将现有函数替换为以下内容:

def post(self):

    user_month = self.valid_month(self.request.get("month"))
    user_day   = self.valid_day(self.request.get("day"))
    user_year  = self.valid_year(self.request.get("year"))

    if not(user_month and user_day and user_day):

        self.response.out.write(form)

    else:

        self.response.write("Thanks! That's a totally calid day!")
def valid_month(self, month):

    if month:

        short_month = month[:3].lower()

        if short_month in self.months_abbvs:

            return True
def valid_day(self, day):

    if day and day.isdigit():

        day = int(day)

        if day > 0 and day <= 31:

            return True
def valid_year(self, year):

    if year and year.isdigit():

        year = int(year)

        if year >= 1900 and year <= 2020:

            return True
对于
valid\u day()
,将现有函数替换为以下内容:

def post(self):

    user_month = self.valid_month(self.request.get("month"))
    user_day   = self.valid_day(self.request.get("day"))
    user_year  = self.valid_year(self.request.get("year"))

    if not(user_month and user_day and user_day):

        self.response.out.write(form)

    else:

        self.response.write("Thanks! That's a totally calid day!")
def valid_month(self, month):

    if month:

        short_month = month[:3].lower()

        if short_month in self.months_abbvs:

            return True
def valid_day(self, day):

    if day and day.isdigit():

        day = int(day)

        if day > 0 and day <= 31:

            return True
def valid_year(self, year):

    if year and year.isdigit():

        year = int(year)

        if year >= 1900 and year <= 2020:

            return True

TypeError:valid_month()正好接受1个参数(给定2个),我通过将函数valid_month等移动到post函数中使其工作。但是,不管输入是否正确,页面都会重新加载表单valid@MMelvin0581这不一定是一种很好的方法,因为每次调用
post()
时,它都会重新定义这些嵌套函数。您肯定希望利用成员函数的
self
关键字,以便它们只定义一次。@MMelvin0581
self.request.get()
必须返回两个值。尝试将
print(self.request.get(“month”)
添加到分配给
user\u month
之前的行中。这会打印出什么?第31行,如果月份较短,则为有效月份:NameError:未定义全局名称“months”。TypeError:valid\u month()正好包含1个参数(给定2个)我通过将函数valid\u month等移动到post函数中来实现它,但是页面不会只是重新加载表单,不管输入是否正确valid@MMelvin0581这不一定是一种很好的方法,因为每次
post()时它都会重新定义这些嵌套函数调用了
。您肯定希望利用成员函数的
self
关键字,以便它们只定义一次。@MMelvin0581
self.request.get()
必须返回两个值。请尝试添加
print(self.request.get(“月”)
到分配给
用户\u月之前的行。打印出什么?如果月数较短,则在有效的\u月中打印第31行:名称错误:未定义全局名称“月”