0
letters1 = "abcdefghijklmnopqrstuvwxyz"
letters2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

def is_valid(strs):
    char_b = True
    for char in range(0, len(strs)):
        if strs[char] not in (letters1 or letters2):
            char_b == False
    return char_b

I don't understand why this won't work, anyone mind giving me a hint? It just always returns true.

5
  • 1
    (letters1 or letters2) is equal to letters1 Commented Feb 4, 2014 at 13:51
  • You may use the string module (in particular string.ascii_lowercase and string.ascii_uppercase) instead of definining manually letters{}. Commented Feb 4, 2014 at 13:52
  • 1
    You might like to know about str.isalpha() method Commented Feb 4, 2014 at 13:58
  • 1
    generally I saw people to misspell == by = but this is first example where typo is opposite!! Commented Feb 4, 2014 at 13:58
  • Using string.ascii_letters would be the way to go. Commented Feb 4, 2014 at 14:01

1 Answer 1

2

You need to set char_b, not test for equality. Replace:

char_b == False

with

char_b = False

Your test is incorrect:

if strs[char] not in letters1 + letters2:

or simplify your function to:

def is_valid(strs):
    return strs.isalpha()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!! I can't believe i missed that

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.