Python Unindent不匹配任何外部缩进级别if语句
我好像有缩进错误Python Unindent不匹配任何外部缩进级别if语句,python,Python,我好像有缩进错误 def date(): #So they don't have to go into IDLE and press F5 if they need to reset due to exceeding the conditionals eg: 1-12 and 1-31 (Dates) We're also not using any parameters to the function. months = {1: "January", 2: "February
def date(): #So they don't have to go into IDLE and press F5 if they need to reset due to exceeding the conditionals eg: 1-12 and 1-31 (Dates) We're also not using any parameters to the function.
months = {1: "January", 2: "February", 3: "March", 4: "April", 5: "May", 6: "June", 7: "July", 8: "August", 9: "September", 10: "October", 11: "November", 12: "December"} #Dictionary. 12 = Key. December = Value.
numberdate = int(input("Enter the day numerically (Ex: \'5' meaning the 5th)\n")) #Prints as a string yet expects return value to be an integer. This is needed to compare later on.
numbermonth = int(input("\nEnter your month numerically (Ex: \'10' meaning October)\n")) #Integer again as the expected return value is an integer.
year = 2014 #Automatically an integer
counter = 1
if numbermonth > 0 and numbermonth < 13: #Compares to the value inputted for numberdate. If it is 1 to 12 it will pass meaning it's accepted. If it is not it will goto the else statement.
pass
else:
print('\nMonth must be between 1 and 12')
print(months, "Type date() to restart!")
if numbermonth == 1: #Checks if month 1 has been inputted (Jan), if so it will proceed to print it with it's actual date, what we have defined it to be.
print("%d/%s/%d" %(numberdate,months[0],year)) #%d = integer, %s = string. Alot quicker than concatenation.
elif numbermonth == 2:
print("%d/%s/%d" %(numberdate,months[1],year)) #Dictionarys, Tuples and Lists all start from an index of 0 making January and 1 February.
elif numbermonth == 3:
print("%d/%s/%d" %(numberdate,months[2],year))
elif numbermonth == 4:
print("%d/%s/%d" %(numberdate,months[3],year))
elif numbermonth == 5:
print("%d/%s/%d" %(numberdate,months[4],year))
elif numbermonth == 6:
print("%d/%s/%d" %(numberdate,months[5],year))
elif numbermonth == 7:
print("%d/%s/%d" %(numberdate,months[6],year))
elif numbermonth == 8:
print("%d/%s/%d" %(numberdate,months[7],year))
elif numbermonth == 9:
print("%d/%s/%d" %(numberdate,months[8],year))
elif numbermonth == 10:
print("%d/%s/%d" %(numberdate,months[9],year))
elif numbermonth == 11:
print("%d/%s/%d" %(numberdate,months[10],year))
elif numbermonth == 12:
print("%d/%s/%d" %(numberdate,months[11],year))
我不确定我做错了什么,我对这门语言还很陌生,我希望能得到一些帮助!我查过其他帖子,他们说一个问题可能会混淆标签和空格,但我不知道如何回忆我使用过的内容或手动检查
if numbermonth == 1: #Checks if month 1 has been inputted (Jan), if so it will proceed to print it with it's actual date, what we have defined it to be.
print("%d/%s/%d" %(numberdate,months[0],year)) #%d = integer, %s = string. Alot quicker than concatenation.
elif numbermonth == 2:
print("%d/%s/%d" %(numberdate,months[1],year)) #Dictionarys, Tuples and Lists all start from an index of 0 making January and 1 February.
没有对齐。正确对齐它们,使
if
匹配elif
和后续语句。如果if这个词前面有一个额外的空格,而你有一个简单的缩进错误,那么这个可怕的if
语句就需要重构了。当检测到numbermonth
超出范围时,从函数返回;然后,您可以通过调用print
来替换整个if
。此外,在列表
同样有效的情况下,也不需要使用dict
。以下行为与函数的行为相同
def date():
months = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
numberdate = int(input("Enter the day numerically (Ex: \'5' meaning the 5th)\n"))
numbermonth = int(input("\nEnter your month numerically (Ex: \'10' meaning October)\n"))
year = 2014
counter = 1
if not (1 <= numbermonth <= 12):
print('\nMonth must be between 1 and 12')
print(months, "Type date() to restart!")
return
print("%d/%s/%d" % (numberdate, months[numbermonth-1], year))
def date():
月份=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”]
numberdate=int(输入(“以数字形式输入日期(例如:\'5'表示第五天)\n”))
numbermonth=int(输入(“\n以数字形式输入您的月份(例如:\'10'表示十月)\n”))
年份=2014年
计数器=1
如果没有(1你并不真的需要所有那些if
和elif
if 1 <= numbermonth <= 12:
print("%d/%s/%d" %(numberdate,months[numbermonth],year))
if 1您的if
语句缩进了一个额外的空格。退格直到它位于左边距并重新缩进。您应该对它下面的打印语句执行相同的操作。
if 1 <= numbermonth <= 12:
print("%d/%s/%d" %(numberdate,months[numbermonth],year))