assert error python code example

Example 1: assert syntax python

assert <condition>,<error message>
#The assert condition must always be True, else it will stop execution and return the error message in the second argument
assert 1==2 , "Not True" #returns 'Not True' as Assertion Error.

Example 2: assert python 3

# Simple asserting thing, run it by using pytest or something 
# If you don't know how to run pytest, then go learn it.

def test_math():
    assert(1 + 1 == 2)

# Another way to test it (without pytest) is:
# You could just run the function to see if it makes an error.
# If it doesn't, it means it was fine, if it does, it means there's an error.

# But then again, using pytest or something is much easier and saves time.
# So try to use testing applications instead of running the function to see.

Example 3: make an assertionerror in python

# ask for a name
answer = input("Please enter your name:")

# make sure it consists of only alphabetical characters (a-z/A-Z)
# assert <condition: bool>, <Error message: str>
assert all([1 if char.isalpha() else 0 for char in answer]) == True, "Please make sure your name consist of only alphabetical characters."