3

Noob wondering how to improve his code.

a, b, c = string.split(enteredDate, "/")

m31s = [1, 3, 5, 7, 8, 10, 12]
m30s = [4, 6, 9, 11]

for x in range(len(m31s)):
    x = int(m31s[x])
    if x != int(a) and b != 31:
         print "Invalid date."
for x in range(len(m30s)):
    et cetera...

In case it isn't clear, I am testing an inputted date to see if it is valid. This is only part of the program. The main question is: what is the best way to test if an element matches ANY element in a list?

My method works... But, I suspect there is a better way to do this. I said boolean in the title because I envision something like:

if secretCode(m31s, int(a)) == True:

Could be a pipedream. Just curious.

Thanks to anyone who takes the time to help.

2
  • 4
    31s is not a valid variable name. variable names can not start with a digit, though they can contain digits. Commented Oct 19, 2012 at 20:09
  • Yeah, you're exactly right. I learned that when I ran this earlier--forgot to make the changes in my post. Good catch. Commented Oct 19, 2012 at 20:13

3 Answers 3

6

You can use the syntax if elem in list. For example:

>>> if 1 in [1,2,3,4,5]:
...     print 'found 1'
... 
found 1
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. This will be prove quite useful!
BTW I chose your answer because it answered the specific question. Other responses also provided excellent insights.
Agreed, they're very helpful. You should upvote them to thank them. (I'm about upvote both as well.)
2

You should use python datetime library.

try:
    datetime.datetime.strptime(enteredDate, "%m/%d/%Y")
except:
    print 'Invalid date'

3 Comments

+1 for datetime. In the OP's code, a appears to be the month, b appears to be a date (31). So perhaps he wants %m/%d/%Y.
Thanks for introducing me to this library. BTW: seems like it should be "%m/%d/%Y"
Wow, missed your comment unutbu. Sorry for the repetition.
1

I suggest:

  • replace

       a, b, c = string.split(enteredDate, "/")
    

    with

       a, b, c = enteredDate.split("/")
    

    Not that it matters much, but the first version needs to import the string module, the second not.

  • replace int(m31s[x]) with m31s[x] (you know those are int already, why the extra int( ... ) then?
  • in case secretCode( ... ) returns true or false you can simply write if secretCode( ... ):, no need to compare with == True.
  • instead of calling int(a), int(b) or int(c) you can simply write a, b, c = map(int, enteredDate.split("\")) and forget about those int( ... ) later since you know they are int. Additionally, if those are not int you will immediately get an exception without unnecessarily progressing in your code.

1 Comment

Thanks for all the style suggestions. Seriously--it is difficult to get these sorts of minor corrections, which are invaluable to me. If only I could get them more often!

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.