我一直在使用udacity.com学习编程一个应用程序,它允许你输入你的年龄,当你点击提交它检查你是否输入了正确的年、月和日。每次我点击提交并在谷歌应用引擎上运行它时,本地主机网页就会变黑。在我的代码中我遗漏了什么地方。我检查了我的代码,它看起来应该可以工作。我需要另一双眼睛盯着这个。
import webapp2
form="""
<form method="post">
What is your birthday?
<br>
<label>Month<input type="type" name="month"></label>
<label>Day<input type="type" name="day"></label>
<label>Year<input type="type" name="year"></label>
<div style="color: red">%(error)s</div>
<br>
<br>
<input type="submit">
</form>
"""
def valid_year(year):
if year and year.isdigit():
year = int(year)
if year > 1900 and year < 2020:
return year
def valid_day(day):
if day and day.isdigit():
day = int(day)
if day > 0 and day <= 31:
return day
months = ['Janurary',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
month_abbvs = dict((m[:3].lower(),m) for m in months)
def valid_month(month):
if month:
short_month = month[:3].lower()
return month_abbvs.get(short_month)
class MainPage(webapp2.RequestHandler):
def write_form(self, error=""):
self.response.out.write(form % {"error": error})
def get(self):
self.write_form()
self.valid_year(year)
self.valid_day(day)
self.valid_month(month)
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_year):
self.write_form("That doesn't look valid to me, friend.")
else:
self.response.out.write("Thanks! That's a totally valid day!")
app = webapp2.WSGIApplication([('/',MainPage)], debug=True)发布于 2015-08-19 05:29:15
我逐字逐句地尝试了你的代码,我得到了下面的错误,它导致了空白屏幕。
ERROR 2015-08-18 21:27:32,377 wsgi.py:263]
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/Users/Josh/main.py", line 1
import webapp2
^
IndentationError: unexpected indent在python中,空格很重要。您的初始import webapp2和month_abbvs = dict((m[:3].lower(),m) for m in months)似乎有一些不一致的缩进。
https://stackoverflow.com/questions/31958785
复制相似问题