我必須在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
我編輯過它 –