Explanation:
Letter Value Mapping: The same dictionary
letter_values
is used to map each letter to its corresponding value.Convert to Lowercase: The input word is converted to lowercase.
Initialize Total: A variable
total
is initialized to store the sum of the values.Iterate Over Characters: The program iterates over each character in the input word.
Check for Valid Characters: It checks if the character exists in the
letter_values
dictionary.Calculate Value: For valid characters, it adds the corresponding value from the dictionary to the
total
.Sum Digits Function: A nested function
sum_digits
is defined to sum the digits of a number repeatedly until a single-digit number is obtained.Calculate Final Value: The
sum_digits
function is called with the total sum to get the final single-digit value.Return Final Value: The function returns the final single-digit value.
This code will prompt the user to enter a word, calculate the sum of its letter values, and then reduce the sum to a single-digit number by repeatedly summing its digits