如果声明

如果声明

问题描述:

Unindent不匹配任何外部缩进级别我似乎正在收到缩进错误。如果声明

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. 

我不知道我做错了,我很新的语言,我希望得到一些帮助!我检查了其他帖子,他们指出一个问题可能会让空间变得混乱,但我不知道如何回忆我所用或手动检查的内容。

+1

您的'if'语句缩进了一个额外的空间。退格键,直到它位于左边距并重新缩进。你应该为它下面的'print'语句做同样的事情。 – jonsca 2014-11-06 23:45:04

 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,其中list也可以正常工作。以下行为与您的功能相同。

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)) 

你并不真的需要所有这些if的和elif的...

if 1 <= numbermonth <= 12: 
    print("%d/%s/%d" %(numberdate,months[numbermonth],year)) 

看到,有没有个月[0]。你正在使用一个字典,其中的关键字是你的numbermonth(从1到12)。您正在使用密钥,而不是索引。