Reputation: 23
I have been assigned to create an application that takes a user's first and second name (making sure they're no longer than 10 characters) and calculate their 'lucky name number' based on this grid:
So for example, John Smith would be:
= (1 + 6 + 8 + 5) + (1 + 4 + 9 + 2 + 8)
= 20 + 24
Then add the digits in each value together:
= (2 + 0) + (2 + 4)
= 2 + 6
= 8 <- Their lucky name number.
Here is the code I have so far:
while True:
first_name = input("What is your first name? ")
if len(first_name) < 10:
print("Hello " + first_name + ", nice name!")
break
else:
print("First name is too long, please enter a shorter name.")
while True:
second_name = input("What is your second name? ")
if len(second_name) < 10:
print ("Wow, " + first_name + " " + second_name + " is a really cool name. Let's see how lucky you are...")
break
else:
print ("Second name is too long, please enter a shorter name.")
However, I am unsure what my next step would be as I need to take each letter in the name and make it a specific value.
The only way I can think of doing this is by listing each letter with its assigned value like this:
A = 1
B = 2
C = 3
D = 4
E = 5
F = 6
G = 7
H = 8
I = 9
J = 1
K = 2
L = 3
M = 4
N = 5
O = 6
P = 7
Q = 8
R = 9
S = 1
T = 2
U = 3
V = 4
W = 5
X = 6
Y = 7
Z = 8
fletter_1 = first_name[0]
fletter_2 = first_name[1]
fletter_3 = first_name[2]
fletter_4 = first_name[3]
fletter_5 = first_name[4]
fletter_6 = first_name[5]
fletter_7 = first_name[6]
fletter_8 = first_name[7]
fletter_9 = first_name[8]
fletter_10 = first_name[9]
print fletter_1 + fletter_2 + fletter_3 + fletter_4 + fletter_5 + fletter_6 + fletter_7 + fletter_8 + fletter_9
But this is an extremely long process and not the best way to code.
Please can someone give me some guidance on how I would complete the next step in the best way possible as I am unsure on what to do.
Upvotes: 1
Views: 3354
Reputation: 3157
Your question is related to Convert alphabet letters to number in Python
Here is some code to compute the Lucky Name Number:
for char in raw_input('Write Text: ').lower():
print (ord(c.lower()) - ord('a')) % 9 + 1)
Or by using list comprehension :
print [(ord(c.lower()) - ord('a')) % 9 + 1 for c in raw_input('Write Text: ')]
Then, to compute the score, you can use sum()
buit-in method on the list:
print sum([(ord(c.lower()) - ord('a')) % 9 + 1 for c in raw_input('Write Text: ')])
Upvotes: 0
Reputation: 637
If you haven't heard of the ASCII table, it's pretty much a convention of how to represent characters in binary. The function ord(c)
in python gives you the value of a given character, according to the ASCII table.
I noticed your Lucky Name Number Grid gives the same value every 9 letters, which in math could be represented as % 9
. Therefore :
# ASCII value of A
>>> ord('A')
65
# Except that we wanted 1 for A
>>> ord('A') - 64
1
# And we also want 1 for J and S
>>> (ord('J') - 64) % 9
1
>>> (ord('S') - 64) % 9
1
>>> (ord('Z') - 64) % 9
8
You can use this last formula : (ord(c) - 64) % 9
Edit : As Loïc G. pointed out, there is a slight error with my formula, due to the modulo function returning 0 from time to time, and your table Grid's indexes starting at 1. Here's the final version :
ord(c.lower()) - ord('a')) % 9) + 1
ord('a')
returns 97
(avoiding to hard-code the value), and c.lower()
makes the function work against lower and upper case characters. The big difference with the first algorithm is that + 1
at the end, shifting all indexes by 1 as required by your grid.
Upvotes: 4
Reputation: 97
# example char dictionary with corresponding integer values - use the Numerical Value Chart
# to get the real values for the whole alphabet. You may need to deal with upper/lower
# case characters
charDict = { 'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4}
# example names - use your own code 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 your 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
Upvotes: 0
Reputation: 387527
You should store your grid as a dictionary:
luckyNameGrid = {
'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9,
'j': 1, 'k': 2, 'l': 3, 'm': 4, 'n': 5, 'o': 6, 'p': 7, 'q': 8, 'r': 9,
's': 1, 't': 2, 'u': 3, 'v': 4, 'w': 5, 'x': 6, 'y': 7, 'z': 8
}
Then, you can use that grid to make the translation into a number (after converting the name to lower-case), and finally, you sum those numbers to get your result:
def getLuckyNameNumber (name):
return sum(map(lambda x: luckyNameGrid[x], name.lower()))
Used like this:
>>> getLuckyNameNumber('John')
20
>>> getLuckyNameNumber('Smith')
24
To make the final conversion, you basically want to calculate the digit sum of each lucky name number. There are various ways to do that. One would be to conver the number into a string, split for each character, convert the characters back to numbers, and sum those:
def getDigitSum (num):
return sum(map(int, str(num)))
An other solution would be to continously divide the number by 10 and sum the remainders. This has the benefit that you don’t need to do type conversions:
def getDigitSum (num):
sum = 0
while num > 0:
num, remainder = divmod(num, 10)
sum += remainder
return sum
For example:
>>> getDigitSum(getLuckyNameNumber('John'))
2
>>> getDigitSum(getLuckyNameNumber('Smith'))
6
Upvotes: 1
Reputation: 101
create a dict from letter to value, call it say, D.
then D['A'] = 1 for example
then do this
def reduce_value(n):
return sum(int(i) for i in str(n))
string = "HEYDUDE"
value = sum(D[letter] for letter in string)
while len(str(value)) > 1:
value = reduce_value(value)
print "final value", value
Upvotes: 0