NameError: global name 'valid_month" is not defined
NameError: global name 'valid_month" is not defined
我正在学习 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)
我 运行 通过 google 应用引擎。当我在浏览器中点击表单提交时,我得到:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
当我从我所在的 运行 服务器查看 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
据我所知,问题出在
user_month = valid_month(self.request.get('月))
行,但我尝试将缩进从空格更改为制表符,然后再更改为空格,重写整个代码并重新启动服务器,但都无济于事。请有人帮助我...
从所属的class内部调用成员函数时,需要在它们前面加上self.
。此外,所有成员函数,如 valid_month()
、valid_day()
和 valid_year()
必须将 self
作为其实际参数的第一个值。此外,任何对静态 class 变量(例如 months
和 months_abbvs
的引用也必须以 self.
开头。
此外,valid_month()
、valid_day()
和 valid_year()
的 return 值可能存在逻辑错误。每个都是 returning 字符串,但 post()
中的条件正在评估所有三个是否都是 True
。此外,valid_months()
中的条件可能是为了检查 short_month
是否在 months_abbvs
中,而不是在 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!")
然后更改以下函数头:
对于 valid_month()
,将现有函数替换为以下内容:
def valid_month(self, month):
if month:
short_month = month[:3].lower()
if short_month in self.months_abbvs:
return True
对于 valid_day()
,将现有函数替换为以下内容:
def valid_day(self, day):
if day and day.isdigit():
day = int(day)
if day > 0 and day <= 31:
return True
对于 valid_year()
,将现有函数替换为以下内容:
def valid_year(self, year):
if year and year.isdigit():
year = int(year)
if year >= 1900 and year <= 2020:
return True
我正在学习 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)
我 运行 通过 google 应用引擎。当我在浏览器中点击表单提交时,我得到:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
当我从我所在的 运行 服务器查看 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
据我所知,问题出在 user_month = valid_month(self.request.get('月)) 行,但我尝试将缩进从空格更改为制表符,然后再更改为空格,重写整个代码并重新启动服务器,但都无济于事。请有人帮助我...
从所属的class内部调用成员函数时,需要在它们前面加上self.
。此外,所有成员函数,如 valid_month()
、valid_day()
和 valid_year()
必须将 self
作为其实际参数的第一个值。此外,任何对静态 class 变量(例如 months
和 months_abbvs
的引用也必须以 self.
开头。
此外,valid_month()
、valid_day()
和 valid_year()
的 return 值可能存在逻辑错误。每个都是 returning 字符串,但 post()
中的条件正在评估所有三个是否都是 True
。此外,valid_months()
中的条件可能是为了检查 short_month
是否在 months_abbvs
中,而不是在 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!")
然后更改以下函数头:
对于 valid_month()
,将现有函数替换为以下内容:
def valid_month(self, month):
if month:
short_month = month[:3].lower()
if short_month in self.months_abbvs:
return True
对于 valid_day()
,将现有函数替换为以下内容:
def valid_day(self, day):
if day and day.isdigit():
day = int(day)
if day > 0 and day <= 31:
return True
对于 valid_year()
,将现有函数替换为以下内容:
def valid_year(self, year):
if year and year.isdigit():
year = int(year)
if year >= 1900 and year <= 2020:
return True