Python, ASCII and your Zodiac

What's your name got to do with your zodiac sign? Probably nothing, but let's relate them with ASCII code for fun. While at it, we will have the opportunity to learn about a few Python beauties like

  • ord
  • enumerate
  • zip
  • dict with zip
  • map

Let's first build an ASCII chart as a list of dictionary items. Python allows multiple ways to do this, so let's start with the built-in function ord().

We will first need a list of the letters of the English alphabet.

alphabet = list('abcdefghijklmnopqrstuvwxyz')

We will be using this list of characters in the subsequent code snippets.

#using ord function
ascii_nums = [{x : ord(x)} for x in alphabet]
print(ascii_nums)

We have used a list comprehension to build a list of dictionary items. Each item is a pair of a letter and its ASCII value.

We will now use the enumerate function to achieve the same result.

# using enumerate
alphanums = list(enumerate(alphabet, 97))
ascii_codes = list(map(lambda x : {x[1] : x[0]}, alphanums))
print(ascii_codes)

The enumerate function is commonly used for indexing elements of a list, but here we have used it to associate ASCII values to the letters.

There is yet another way to do the same thing using dict and zip together. For this to work we need a list of numbers to associate with letters.

numbers = list(x for x in range(97, 123))

This list of numbers we will again use in the following code snippets.

# using dict, zip
ascii_vals = [dict(zip(alphabet, numbers))]
print(ascii_vals)

The zip function creates tuples of elements, one from each of the two lists supplied to it. The dict function converts the tuples into dictionary items. The index operator [ ] appends the items into a list.

Map

The ASCII chart can also be built using the map function. Here is the snippet to do that.

#using map
ascii_d = list(map(lambda a : {a[0] : a[1]}, zip(alphabet, numbers)))

print(ascii_d)

The map is a function that takes two arguments: a function and an iterable.

We have used a lambda function, also called anonymous function, to build a dictionary of items. Recall that the item here is a tuple of letter and number from the zip function.

a in the lambda function is the tuple and the two elements in the tuple are accessed using the index operator.

ASCII vs Zodiac

Now, it is time to code our program to see how close our name is to our zodiac sign using the ASCII chart.

Type or copy-paste the following into a file ascii_score.py and run the program. I wrote it in the Pydroid 3 app on my phone.

Uncomment print statements if you want to see what is going on.


alphabet = list('abcdefghijklmnopqrstuvwxyz')
numbers = list(x for x in range(97, 123))
#using map
ascii_list = list(map(lambda a : {a[0] : a[1]}, zip(alphabet, numbers)))
# print(ascii_list)

# function to return ASCII code of the letter passed add argument.
def get_ascii(letter):
    for pair in ascii_list:
        if letter in pair:
            return pair[letter]

# function to calculate the score
def process(word):
    word = word.strip().lower()
    total = 0
    #print(word)
    for letter in word:
        num = get_ascii(letter)
        #print(f'{letter}:{num}')
        total += num
    return total


print('Let us see how your name compares with your zodiac sign.')

name = input('Enter your name: ')
star = input('Enter your zodiac sign: ')

name_score = process(name)
star_score = process(star)

print(f'Your name score is {name_score}')
print(f'Your zodiac score is {star_score}')
ascii_score = abs(name_score - star_score) #abs function to ignore negative sign
print(f'They differ by {ascii_score} points on the ASCII chart.')

Exercise Set some range of values for the difference in the ascii_score to give your user some consolation if the difference is great.

Enjoy coding!