Python

Checks if something is a palindrome.

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
print(r"""
   ___      _ _           _           
  / _ \__ _| (_)_ __   __| |_ __ __ _ 
 / /_)/ _` | | | '_ \ / _` | '__/ _` |
/ ___/ (_| | | | | | | (_| | | | (_| |
\/    \__,_|_|_|_| |_|\__,_|_|  \__,_|

Palindrome detector - by Kz""")

while True:

    # capture
    try:
        str_input = input("\nPlease enter your phrase: ").lower()
    except KeyboardInterrupt:
        print("\nEnter 'exit' to quit.")
        continue

    # handle exit
    exits = ["exit", "quit", ":q", ":e"]
    if str_input in exits:
        print("Bye bye!\n")
        exit()

    # clean
    str_cleaned = ""
    for char in str_input:
        if char.isalnum():
            str_cleaned += char

    # lenght validation
    total_len = len(str_cleaned)
    if total_len < 3:
        print("Too short!\n")
        continue

    halve_len = int(total_len / 2)
    part_1 = str_cleaned[0:halve_len]
    part_2 = str_cleaned[total_len-halve_len:]

    # reverse
    part_2 = part_2[::-1]

    if part_1 == part_2:
        print("This is a palindome!")
    else:
        print("Noop.")