我必须在python中创建一个幸运数字程序,但我不断遇到很多错误。 如果你不知道一个幸运的名字是个什么数字,它基本上是在每个字母都有一个值,你添加值toether您的名字和第二名称,以便例如 李四在Python中幸运数字挑战
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
所以我的问题是,是我在哪里可以从这里走,因为数字不正确加起来和字母的值不正确,所以基本上我该如何正确计算
什么是你的问题? – aberger
我编辑过它 –