Slice error in python (´ ; ω ; `)

An error has occurred! !! !!

In python, I wrote a method to repel an invalid email address.

def isValidEmail(email):
    if email[0] == '@':
        return False
    elif ' ' in email:
        return False
    elif email.count('@') > 1:
        return False
    elif email['@':].count('.') < 1:
        return False
    else:
        return True

I wanted to add a condition to repel an email address that does not have a "." After the 4th "@" in the condition, but an error occurred.

The error content is "slice indices must be integers or None or have an __ index __ method" It was. Apparently the contents of the slice need to be an integer.

solution

I prepared a variable that cuts out the domain name after @ in advance, and decided to search for "." In it. success!

def isValidEmail(email):
    domain = email.find('@')
    if email[0] == '@':
        return False
    elif ' ' in email:
        return False
    elif email.count('@') > 1:
        return False
    elif email[domain:].count('.') < 1:
        return False
    else:
        return True

It may not be the ultimate solution, but it's a solution for the time being!

Recommended Posts

Slice error in python (´ ; ω ; `)
Python slice
[Python] Reason why index error does not occur in slice
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
Python Error Handling
N-Gram in Python
Programming in python
Python slice basics
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Error in TensorBoard
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
Import Error in Python3: No module named'xxxxx'
flatten in python
Resolve Japanese write error UnicodeEncodeError in Python files
To write to Error Repoting in Python on GAE
Use a custom error page in python / tornado
Error when trying to install psycopg2 in Python
[python] ERROR: Python headers are missing in /usr/include/python3.X m.
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python