We need to import the string module
import string
Get the user input and lower the case for further processing.
sentence = input("Enter your Name: ")
print ("\n You Entered as : \n", sentence.lower())
We are using the for loop and string function ‘ascii_lowercase’ for generating the alphabets in lower case.
alpha={}
for i in range(0,26):
a = list(string.ascii_lowercase)
alpha.update({a[i]:i+1})
List of numbers assigned to a variable.
d = ['1','2','3','4','5','6','7','8','9','0']
Initializing the variable with ‘0’
count = result = digit = 0
Implementing a for loop that calculates the total count/number for the given name (that might include numeric values too)
The given input expected to contain characters and numeric values, hence we implemented an if condition based on which we,
Calculate the total count/ number
for char in sentence:
if char in alpha:
count = count + alpha[char]
elif char in d:
count = count + int(char)
print("\n Total Number for given Name : ", count)
Whatever the evaluated number/count is initialized to variable ‘num’
num = count
So, here if we get the result of total number/count which contain more than one digit like 23 or 235 etc…
Then all digits in that number has to be added until we get the final lucky number in the range of 0-9.
while (num > 0) :
digit += num%10
#result = result + digit
num = num//10
if (num == 0 and digit > 9):
num = digit
digit = 0
print("\nYour Lucky Number is : ", digit)
Below is the complete code:
import string
sentence = input("Enter your Name: ")
print ("\n You Entered as : \n", sentence.lower())
alpha={}
for i in range(0,26):
a = list(string.ascii_lowercase)
alpha.update({a[i]:i+1})
d = ['1','2','3','4','5','6','7','8','9','0']
count = result = digit = 0
for char in sentence:
if char in alpha:
count = count + alpha[char]
elif char in d:
count = count + int(char)
print("\n Total Number for given Name : ", count)
num = count
while (num > 0) :
digit += num%10
#result = result + digit
num = num//10
if (num == 0 and digit > 9):
num = digit
digit = 0
print("\nYour Lucky Number is : ", digit)