I am trying to write code that compares two strings and returns a string if a match is found with a case-sensitive condition, with the exception of capital. That function that I wrote, and I found out that == is pretty good for comparing the case sensitively. However, it still prints January for the last test line, which is not expected. So can you help me?
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] def valid_month(month): for x in months: if x==month.capitalize() : print x
Test Codes:
valid_month("january") valid_month("January") valid_month("foo") valid_month("") valid_month("jaNuary")
source share