1

I'm building a simple email verifier. I need to compare the local-parts current letter to a list of valid characters. So essentially I'm asking how do I check to see if the current letter I'm on in local-part is equivalent to a letter in the ENTIRE list of valid chars. If it is a valid character, local-part will go to the next letter in its string and go through the list of valid characters to see if this too is and so on until it reaches the @ symbol unless there isn't a valid character.

I'm fairly new to python so I don't know how nested for loops work.

for ch in local:
    for ch in valChar:
    if(ch ==ch) <----problem

This is what I currently have written for the loops. Is "ch" a variable or some type of syntax to represent char?

1
  • use a different local variable in the inner for loop, and check against that.. example for x in valChar Commented Jan 27, 2015 at 22:28

5 Answers 5

4

You don't need nested loop in this case, thanks to the in operator:

for c in local:
    if c in valChar:
        performvalidaction(c)
    else:
        denoteasinvalid(c)

What identifier to use (c, ch, or anything else) is pretty indifferent, I tend to use single-character identifiers for loop variables, but there's no rule saying that you must.

If you did have to use two nested loops, you'd just use different loop variables for the two loops.

In fact you don't even need one loop here (you could instead work e.g with Python's sets, for example) -- much less two -- but I guess using one loop is OK if it's clearer for you.

Sign up to request clarification or add additional context in comments.

3 Comments

@MalikBrahimi, a RE is just another non-loop alternative (like the one I mention about set, which BTW is a concept more likely to be familiar -- some set theory is often taught in school, regular expressions usually aren't:-). I repeat: one doesn't have to use a loop, but if one finds it simplest/clearest to loop, that's OK too.
I see what you mean. In some instances though, loops can have their limitations. Regex has a far wider range of capabilities.
@MalikBrahimi, and sets a far wider range than regexes -- the latter are only useful to deal with strings, sets are far broader in application. So what? In this case, a loop (probably simplest for the OP) is fine, so would a set be, so would a regex be. Let the user pick whatever's simplest to them and adequate to solve this problem, not wonder about other problems they're not currently facing.
0

ch is a variable, you can replace it with any valid identifier:

for local_ch in local:
    for valChar_ch in valChar:
        if(local_ch == valChar_ch): <----No problem

Comments

-1

Let me explain the for loop for you:

for eachitem in file:
    do something

eachitem is a variable of one specific value of a file/dictionairy etc..

Comments

-1

I discovered an efficient solution using Python's zip function, which pairs corresponding elements from multiple sequences.

Example:

string_1 = 'ABC'
string_2 = '123'
result = list(zip(string_1, string_2))
# Output: [('A', '1'), ('B', '2'), ('C', '3')]

Comments

-3

You need to validate an email address, i would use a regular expression: \b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,6}\b

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.