Reputation: 46
I seem to be getting an indentation error.
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 is currently highlighting past this comment on:
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.
I'm not sure what I've done wrong, I'm pretty new to the language, I would appreciate some assistance! I have checked other posts and they state an issue may be confusing tabs with spaces however I am not aware on how to recall what I used or manually check.
Upvotes: 0
Views: 314
Reputation: 2562
You don't really need all those if
's and elif
's...
if 1 <= numbermonth <= 12:
print("%d/%s/%d" %(numberdate,months[numbermonth],year))
See that there is not a months[0]. You are using a dict with the key being your numbermonth (from 1 to 12). You are using a key, not an index.
Upvotes: 0
Reputation: 530970
While you have a simple indentation error, that monstrous if
statement is crying out to be refactored. Return from the function when detect that numbermonth
is out of range; then you can replace the entire if
with a single call to print
. Also, there's no need to use a dict
where a list
will work just as well. The following behaves identically to your function.
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))
Upvotes: 0
Reputation: 2926
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.
aren't aligned. Align them properly so if
matches elif
and subsequent statements.
Upvotes: 1