2

I have:

*MONTHS = ("January", "February", "March", ... "December") (all months included)

I'm supposed to input the 3 letter abbreviation for a month and get the index value for the month. So far, I have:

for M in MONTHS:
    shortMonths = M[0:3]
    print shortMonths

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

I noticed that the output months in shortMonths do not have quotation marks, which is making it impossible to test if an abbreviation is in shortMonths:

MMM = "Feb"

print list(shortMonths).index(MMM) + 1 # taking into consideration that the first month of the list, January, is month 0+1 = 1 and so on for all months

ValueError: 'Feb' is not in list

How can I fix this without creating a function? Also, this is an assignment question. And, we're not allowed to use dictionaries or maps or datetime

3
  • 2
    Try print 'hi' in your interpreter and the Mystery of the Missing Quotation Marks may become solved. Commented Oct 17, 2013 at 6:03
  • This is not a real Python question... more about programming in general (i.e. what 'print' means!). I suggest to start learning how to debug your program so that you will be able to find mistakes and learn from them. Commented Oct 17, 2013 at 8:39
  • @roippi, the Mystery of the Missing Quotation Marks is cool :) Commented Oct 17, 2013 at 9:30

5 Answers 5

2

It sounds like you want shortMonths to be a list, but you're just assigning a string to it.

I think you want something like this:

shortMonths = [] # create an empty list
for M in MONTHS:
    shortMonths.append(M[0:3]) # add new entry to the list
print shortMonths # print out the list we just created

Or using a list comprehension:

# create a list containing the first 3 letters of each month name
shortMonths = [M[0:3] for M in MONTHS]
print shortMonths # print out the list we just created
Sign up to request clarification or add additional context in comments.

1 Comment

Points out the OP's misunderstanding. Gives a pointer to list comp. +1 from me.
0

shortMonths is string not a list. Do as below.

shortMonths = []
  for M in MONTHS:
    shortmonths.append(M[0:3])

Comments

0
>>> months = ["January", "Febuary", "March", "April"]
>>> search = "Mar"
>>> [i for i, mo in enumerate(months) if search == mo[0:3]][0]
2
>>> search = "Fan"
>>> [i for i, mo in enumerate(months) if search == mo[0:3]][0]
IndexError: list index out of range

Comments

0

It's easy:

>>> months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
>>> search = "Jan"
>>> months.index([i for i in months if i[:3].lower() == search.lower()][0])+1
1
>>> # Or
... def getmonth(search):
...    for i in months:
...        if i[:3].lower() == search.lower():
...            return x.index(i)+1
>>> getmonth("Feb")
2

To catch the error:

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
while True:
    search = ""
    while len(search) != 3:
        search = raw_input("Enter first 3 letters of a month: ")[:3]
    month = [i for i in months if i[:3].lower() == search.lower()]
    if not month: # Not in the list, empty list: []
        print "%s is not a valid abbreviation!"%search
        continue
    print months.index(month[0])+1

Comments

0
abbr = raw_input("Enter the month abbr:")


months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
months_abbr = [month.lower()[:3] for month in months]
index = months_abbr.index(abbr.lower())
if index >= 0:
    print "abbr {0} found at index {1}".format(abbr, index)
else:
    print "abbr {0} not found".format(abbr)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.