由于这个错误,我在学校的程序上停留了一个多小时,而且我似乎不知道该如何解决。
导入系统
myDate类:
#Constructer
def __init__(self, month, day, year):
self.day = day #Privet day
self.month = month #Privet month
self.year = year #privet year
#format for MDYYYY
def MDYYYY(self):
print("%i/%i/%i" %(myDate.month, myDate.day, myDate.year))
#format for MDYY
#format for YYYYMD
#mutator to set Day of the month
def setDay(self, a):
try:
day = int(a)
if day < 1 or day > 31:
raise Exception("invalid day")
except Exception as err:
print("Invalid day")
sys.exit( err )
else:
self.__day = day
#Mutator to set Month of the year
def setMonth(self, a):
try:
month = int(a)
if month < 1 or month > 12:
raise Exception("invalid month")
except Exception as err:
print("Invalid month")
sys.exit( err )
else:
self.__month = month
#Mutator to set the year of the date
def setYear(self, a):
try:
day = int(a)
if day < 1700:
raise Exception("invalid year")
except Exception as err:
print("Invalid year")
sys.exit( err )
else:
self.__year = year
# accessor to get day
def getDay(self):
return self.__day
# accessor to get the month
def getMonth(self):
return self.__month
# accessor to get the year
def getYear(self):
return self.__year
if __name__ == "__main__":
print()
print( "Instantiating object today and sorting value using .setMonth(6) .setDay(17) .setYear(2019)" )
setDay.day = 17
setMonth.month = 6
setYear.year = 2019
print(myDate.MDYYYY())
ime在运行它时得到以下结果: 今天实例化对象并使用.setMonth(6).setDay(17).setYear(2019)对值进行排序 追溯(最近一次通话): 文件“ D:\ RCCC \ Fall 2020 \ Python GUI \ Project 1 \ dateformat_duxbury.py”,第8行,在 myDate()类: 在myDate中的文件“ D:\ RCCC \ 2020秋季\ Python GUI \ Project 1 \ dateformat_duxbury.py”,第80行 打印(myDate.MDYYYY()) NameError:未定义名称“ myDate”
答案 0 :(得分:1)
从帖子格式的外观看,似乎您的方法定义与myDate类声明在同一缩进级别。这意味着它们不是全局类的方法,而是类的方法。您应该缩进所有这些内容,以使它们比类myDate的声明深一层。这解释了“未定义名称'myDate'”错误。
还有更多错误:
示例:
date = myDate()
date.setDay(17)
date.setMonth(6)
date.setYear(2019)
请不要误解,但听起来您应该复习有关Python中类如何工作的教程。
答案 1 :(得分:1)
欢迎堆栈溢出。
您的问题似乎更多是概念上的问题,而不是实施上的问题。我强烈建议您阅读面向对象的原理,因为您似乎混淆了类和对象。
例如,您的特定错误与代码的这一部分有关:
#format for MDYYYY
def MDYYYY(self):
print("%i/%i/%i" %(myDate.month, myDate.day, myDate.year))
但是,您是否考虑过该函数中是否有myDate实例?实际上,您要访问的是要从中调用MDYYYY()的特定对象的月,日和年。在这种情况下,您需要访问对象自己的月,日和年变量。为此,您可以使用self
关键字访问对象的内部变量。
#format for MDYYYY
def MDYYYY(self):
print("%i/%i/%i" %(self.month, self.day, self.year))
现在,第二个问题是在主代码块中实例化一个对象。
if __name__ == "__main__":
print()
print( "Instantiating object today and sorting value using .setMonth(6) .setDay(17) .setYear(2019)" )
# Instantiate a myDate object here:
myDateObject = myDate(4, 25, 2005) # You need to pass in the month, day and year values as part of your class init function
# You call instance functions on the object itself, such as
myDateObject.setDay(7)
myDateObject.setMonth(6)
myDateObject.setYear(2019)
# Call the MDYYYY function on the myDateObject instance and not the myDate class
print(myDateObject.MDYYYY())
同样,我强烈建议您回顾一些OOP基础知识。希望这会有所帮助。
答案 2 :(得分:0)
目前尚不清楚为什么在main
部分中访问类方法而不先实例化类。您的评论说您正在实例化该类,但实际上并非如此。应该是这样的:
if __name__ == "__main__":
md = myDate(1, 1, 1900)
md.setDay(17)
md.setMonth(6)
md.setYear(2019)
好的,然后以一种奇怪的方式定义了MDYYYY()
方法。这是类myDate
的方法,但是由于某种原因,您引用的是类,而不是简单地使用self
:
def MDYYYY(self):
print("%i/%i/%i" %(myDate.month, myDate.day, myDate.year))
#Should probably be defined instead like
def MDYYYY(self):
print(f'{self.month}/{self.day}/{self.year}')
此外,您在缩进方面遇到问题,并且您的类/方法名不是Python的(请参见PEP 8)。
答案 3 :(得分:0)
因此,如果其他人遇到问题,这里是我用来解决问题的最终代码,感谢那些发表评论并帮助我的人。我也不知道为什么它会在代码中显示导入和类,但是所有功能都被制表了。
`导入系统
myDate类:
#Constructer
def __init__(self, month, day, year):
self.day = day
self.month = month
self.year = year
#format for MDYYYY
def MDYYYY():
print(f'{myDate.month}/{myDate.day}/{myDate.year}')
#format for MDYY
def MDYY():
print(f'{myDate.month}/{myDate.day}/{myDate.year}')
#format for YYYYMD
def YYYYMD():
print(f'{myDate.year}/{myDate.month}/{myDate.day}')
#mutator to set Day of the month
def setDay(self, a):
try:
day = int(a)
if day < 1 or day > 31:
raise Exception("invalid day")
except Exception as err:
print("Invalid day")
sys.exit( err )
else:
self.__day = day
#Mutator to set Month of the year
def setMonth(self, a):
try:
month = int(a)
if month < 1 or month > 12:
raise Exception("invalid month")
except Exception as err:
print("Invalid month")
sys.exit( err )
else:
self.__month = month
#Mutator to set the year of the date
def setYear(self, a):
try:
day = int(a)
if day < 1700:
raise Exception("invalid year")
except Exception as err:
print("Invalid year")
sys.exit( err )
else:
self.__year = year
# accessor to get day
def getDay(self):
return self.__day
# accessor to get the month
def getMonth(self):
return self.__month
# accessor to get the year
def getYear(self):
return self.__year
如果名称 ==“ 主要”: 打印() print(“今天实例化对象并使用.setMonth(6).setDay(17).setYear(2019)排序值”) myDate.day = 17 myDate.month = 6 myDate.year = 2019
# Print the date format for MM/DD/YYYY (01/01/2001)
print('Showing date in format- MM/DD/YYYY: ', end = '')
print(myDate.MDYYYY())
print("Instantiating object today and sorting value using .setMonth(7) .setDay(18) .setYear(2021)")
myDate.day = 18
myDate.month= 7
myDate.year = 2021
if myDate.year > 1699 and myDate.year < 1800:
myDate.year = myDate.year - 1700
elif myDate.year > 1799 and myDate.year < 1900:
myDate.year = myDate.year - 1800
elif myDate.year > 1899 and myDate.year < 2000:
myDate.year = myDate.year - 1900
elif myDate.year > 1999 and myDate.year < 2100:
myDate.year = myDate.year - 2000
# Print the date fomat for MM/DD/YY (01/01/01)
print('Showing date format - MM/DD/YY: ', end = '')
print(myDate.MDYY())
myDate.year = 2021
# Prints the date format for YYYY/MM/DD (2001/01/01)
print('Showing date format - YYYY/MM/DD: ', end ='')
print(myDate.YYYYMD())
print()`