我必须在python中创建一个幸运的名字编号程序,但我一直遇到很多错误。 如果你不知道一个幸运的名字是什么,它基本上是字母表中的每个字母都有一个值,你在你的名字和第二个名字中加上toether值,例如 约翰多伊
165 465 1+6+5 = 12 4+6+5 = 15 15 + 12 = 27 2+7 = 8 then 8 = has diplomatic skills
这是我到目前为止所做的:
#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
所以我的问题是,我从哪里开始,因为数字没有正确加起来并且字母的值不正确,所以基本上我如何正确地进行计算< / p>
答案 0 :(得分:0)
考虑到你总是将你的数字分成数字级别,我会让你把它包装在一个函数中并修改字典以添加其他字母
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)