Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;名称错误:名称';自我';“未定义”;_Python - Fatal编程技术网

Python &引用;名称错误:名称';自我';“未定义”;

Python &引用;名称错误:名称';自我';“未定义”;,python,Python,我肯定这与标签/空格有关,但100万美元的问题是程序在哪里 import webapp2 form=""" <form method="post"> What is your birthday? <br> <label> Month <input type="text" name="month"> </label> <label> Day <input ty

我肯定这与标签/空格有关,但100万美元的问题是程序在哪里

import webapp2

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>
    <div style="color: red">%s(error)s</div>
    <br>
    <br>
    <input type="submit">
</form>
"""
months = ['January', 'Febuary','March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
mapping = dict((m[:3].lower(), m) for m in months)

def valid_month(month):
    if month:
        s_month = month[:3].lower()
        return mapping.get(s_month)

def valid_day(day):
    if day and day.isdigit():
        day = int(day)
        if day in range(1, 32):
            return day

def valid_year(year):
    if year and year.isdigit(): 
        int_year = int(year)
        if int_year in range(1900,2021): 
            return year

class MainPage(webapp2.RequestHandler):
    def write_form(self, error=""):
        self.response.out.write(form % {"error": error})

    def get(self):
        self.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_year):
            self.write_form("That's wasn't valid, friend!")
        else:
            self.response.write("Thanks! That's a totally valid day!")

app = webapp2.WSGIApplication([('/', MainPage)], debug=True)            
导入webapp2
form=”“”
你的生日是什么?

月 白天 年 %s(错误)s

""" 月份=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’、‘十一月’、‘十二月’] mapping=dict((m[:3].lower(),m)表示m(以月为单位) def有效月份(月): 如果月份: s_月=月[:3]。下() 返回映射。获取(s_月) def有效日(天): 如果日复一日.isdigit(): 日=整数(日) 如果日期在范围(1,32)内: 回归日 def有效年(年): 如果年复一年。isdigit(): 整数年=整数(年) 如果整数年在范围内(19002021): 回归年 类主页(webapp2.RequestHandler): def写入表单(self,error=”“): self.response.out.write(格式%{“error”:error}) def get(自我): self.write_form() def post(自我): 用户月份=有效月份(self.request.get('month')) user\u day=valid\u day(self.request.get('day')) 用户年=有效年(self.request.get('year')) 如果不是(用户月、用户日和用户年): self.write_表单(“这是无效的,朋友!”) 其他: self.response.write(“谢谢!这是完全有效的一天!”) app=webapp2.WSGIApplication([('/',主页)],debug=True)
python-tt
将告诉您在哪里

$ python -tt script.py
  File "script.py", line xxx
    ...
TabError: inconsistent use of tabs and spaces in indentation

您需要在类中定义有效月份、有效日期和有效年份

当你打电话给他们时,你需要说

 user_month = self.valid_month(self.request.get('month'))
而不是

 user_month = valid_month(self.request.get('month'))
def valid_month(month):
在每个方法的每个定义中,您需要包含“self”作为第一个参数,以便它将绑定到类:

def valid_month(self, month):
而不是

 user_month = valid_month(self.request.get('month'))
def valid_month(month):

否则,类所指的“self”对于类范围之外的这些无关方法来说没有任何意义。

在哪一行
self
没有定义?其中的所有“self”看起来都是有效的。你能不能给我们脚本中有问题的部分(带有引发异常的行的函数)?我怀疑输入错误…self未在“user\u day=valid\u day(self.request.get('day')”中定义”