Unindent与statement的任何外部缩进级别都不匹配

2024-10-03 11:22:09 发布

您现在位置: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", 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))

My IDLE当前正在突出显示此评论:

^{pr2}$

我不知道我做错了什么,我对这门语言还不太熟悉,我会很感激你的帮助!我已经检查了其他帖子,他们说一个问题可能是混淆标签与空格,但我不知道如何回忆我使用或手动检查。在


Tags: andthetoandateifisit
3条回答
     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.

没有对齐。将它们正确对齐,以便ifelif和后续语句匹配。在

当您有一个简单的缩进错误时,那个可怕的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前面有一个多余的空格。在

相关问题 更多 >