Ryan B
Ryan B

Reputation: 108

Lucky Name Number Challenge In Python

i have to create a lucky name number programme in python but i keep coming across many errors. if you don't know what a lucky name number is, it is basically where each letter of the alphabet has a value and you add the values toether in your first name and second name so for example john doe

    165  465
    1+6+5 = 12
    4+6+5 = 15
    15 + 12 = 27
    2+7 = 8
    then 8 = has diplomatic skills

Here is what i have done so far:


     #this will go all the way to z
    charDict = { 'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4} 

    # example names - while loop will go here
    firstName = 'AAB'
    lastName = 'DCDD'

    # split the strings into a list of chars
    firstNameChars = list(firstName)
    lastNameChars = list(lastName)

    # sum up values
    firstNameSum = 0
    lastNameSum = 0
    for chr in firstNameChars:
        firstNameSum += charDict[chr]
    for chr in lastNameChars:
        lastNameSum += charDict[chr]

    # cast sums to strings. In this example, this would be '2024'
    combinedNames = str(firstNameSum) + str(lastNameSum)

    # split the string into a list of chars
    combinedNameDigits = list(combinedNames)

    # sum them up
    finalSum = 0
    for dgt in combinedNames:
        finalSum += int(dgt)

    # print the lucky number
    print finalSum

So my question is, is where do i go from here, as the numbers don't add up correctly and the values of the letters aren't correct, so basically how do i do the calculations correctly

Upvotes: 1

Views: 1490

Answers (2)

RysDe
RysDe

Reputation: 93

This works considering you always split your numbers to the digit level, I let you wrap it in a function and modify the dictionary to add other letters

first = 'aaa'
last = 'bbb'
name = first + last
dicti = {'a':1, 'b':2, '1':1, '2':2 ,  '3':3 ,  '4':4 ,  '5':5 ,  '6':6 ,
            '7':7 ,  '8':8 ,  '9':9 ,  '0':0}
while len(name) >1:
    sum = 0
    for letter in name:
        sum = sum + dicti[letter]
    name = str(sum)
final_sum = int(name)

Upvotes: 0

user4624500
user4624500

Reputation: 316

I really don't undersand how: john doe gives 165 465 and how: AAB DCDD gives 2024. However, the standard way to convert letters in numbers and to sum the digits of a number is the following:

def letters_to_numbers(name):
    sum_ = 0
    for letter in name:
        sum_ += ord(letter.upper())-64 #ord("A")=65 minus 64 -> 1
    return sum_

def sum_digits(number):
    sum_ = 0
    while number:
        sum_ += number%10
        number //=10
    return sum_

sum_digits(
    sum_digits(letters_to_numbers("john"))
   +sum_digits(letters_to_numbers("doe")))

Upvotes: 2

Related Questions