[英]python object creation in 3.6.0
我剛剛介紹了python中的Classes。程序用於計算給出日期的新日期和添加到日期的天數。 我正在運行給定的代碼,但我收到無效的語法錯誤,但沒有指定行號。 誰能解釋有什么不對? 謝謝! 代碼在python 3.6.0中
class Date(object):
months={1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
def __init__(self):
self.date=0
self.month=0
self.year=0
self.leap=False
self.enter()
def enter(self):
self.date=int(input("Enter the date: "))
self.month=int(input("Enter the month: "))
self.year=int(input("Enter the year: "))
if self.year%4==0:
if self.year%100==0:
break
self.leap=True
months[2]=29
def __add__(self,day=int(input("Enter the number of days: "))):
self.date+=day
c=0
for i in range(12):
self.date-=Date.months[i+1]
if self.date<1:
self.date+=Date.months[i+1]
break
c+=1
self.month+=c
if self.month>12:
self.year+=1
self.month=self.month//12
print(self.date,'/',self.month,'/',self.year
#main
ob1=Date()
您應該在您的代碼中修復縮進問題:
class Date(object):
months={1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
def __init__(self):
self.date=0
self.month=0
self.year=0
self.leap=False
self.enter()
def enter(self):
self.date=int(input("Enter the date: "))
self.month=int(input("Enter the month: "))
self.year=int(input("Enter the year: "))
if self.year%4==0:
if self.year%100==0:
pass
self.leap=True
months[2]=29
def __add__(self,day=int(input("Enter the number of days: "))):
self.date+=day
c=0
for i in range(12):
self.date-=Date.months[i+1]
if self.date<1:
self.date+=Date.months[i+1]
break
c+=1
self.month+=c
if self.month>12:
self.year+=1
self.month=self.month//12
print(self.date,'/',self.month,'/',self.year)
#main
ob1=Date()
此外,您不能在for
循環外使用break
。 將其更改為pass
或其他內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.