我似乎得到了缩进错误。
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))
我的IDLE目前正在突出显示以下评论:
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.
我不确定我做错了什么,我对这门语言很陌生,我很感激一些帮助!我已检查过其他帖子并说明问题可能会使标签与空格混淆但是我不知道如何回忆我使用的或手动检查。
答案 0 :(得分:1)
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
及后续陈述。
答案 1 :(得分:1)
在单词if
之前有一个额外的空格。
答案 2 :(得分:0)
当你有一个简单的缩进错误时,那个怪异的if
语句迫不及待重构。检测到numbermonth
超出范围时从函数返回;然后,您只需拨打一次if
即可替换整个print
。此外,还不需要使用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))
。以下行为与您的功能相同。
{{1}}
答案 3 :(得分:0)
你真的不需要所有if
和elif
的......
if 1 <= numbermonth <= 12:
print("%d/%s/%d" %(numberdate,months[numbermonth],year))
看到没有几个月[0]。你正在使用一个dict,键是你的numbermonth(从1到12)。您使用的是密钥,而不是索引。