如何使所有数字加在一起,直到左边有一个数字(在列表中)



假设程序询问您的全名并将其转换为数字,然后它将继续在列表中添加数字,直到得到一个1位数的数字。我已经完成了这个产品,工作得很好,但它不能添加第三个或更多的数字。请帮忙。它说在链接中我需要帮助的地方

name1 = []  #its a list called name  
name1 = str(raw_input("Enter your first name and second name "))   #this code asks the user and saves the name as a list  
lettervalue =[]   
index=0  
#

将名称转换为数字并将其放入列表的代码

for index in range (len(name1)):                
  if name1 [index] == "a" or name1 [index] == "j" or name1 [index] == "s":                             
        lettervalue.append(1)                     
elif name1 [index] == "b" or name1 [index] == "k" or name1 [index] == "t":  
        lettervalue.append(2)
elif name1 [index] == "c" or name1 [index] == "l" or name1 [index] == "u":
    lettervalue.append(3)
elif name1 [index] == "d" or name1 [index] == "m" or name1 [index] == "v":
    lettervalue.append(4)
elif name1 [index] == "e" or name1 [index] == "n" or name1 [index] == "w":
    lettervalue.append(5)
elif name1 [index] == "f" or name1 [index] == "o" or name1 [index] == "x":
    lettervalue.append(6)
elif name1 [index] == "g" or name1 [index] == "p" or name1 [index] == "y":
    lettervalue.append(7)
elif name1 [index] == "h" or name1 [index] == "q" or name1 [index] == "z":
    lettervalue.append(8)
elif name1 [index] == "i" or name1 [index] == "r":
    lettervalue.append(9)
elif name1 [index] == " ":
    lettervalue.append(0)
index = index + 1
#
print lettervalue #prints the list to reduce confusion   
total1 = sum(lettervalue) #sums up the numbers in the list and turns it into a variable  
print total1     # also prints the total to reduce confusion   
#

一个while循环,不断将每个数字相加,直到它左边有一个数字(我需要帮助的地方)

while total1 > 9:
split1 = 0
split2 = 0
total1 = str (total1)
split1 = total1[0]
split2 = total1[1]
total1 = int (split1) + int(split2)
print "your lucky number is " + str(total1)
#

该代码根据其特征生成最终数字并打印出来,让他们知道自己的幸运数字

if total1 == 1:
print "WOW you are a Natural leader!"
if total1 == 2:
print "WOW you are a Natural peacemaker!"
if total1 == 3:
print "WOW you are Creative and optimistic !"
if total1 == 4:
print "WOW you are a Hard worker!"
if total1 == 5:
print "WOW you Value freedom!"
if total1 == 6:
print "WOW you one of them who cares and provides!"
if total1 == 7:
print "WOW you are a Think!"
if total1 == 8:
print "WOW you have diplomatic skills!"
if total1 == 9:
print "WOW you are selfless and generous!"

以下代码解决while循环的问题:

totals = []
total = sum(lettervalue)
while total > 9:
    breakdown = list(str(total))
    for i in breakdown:
        i = int(i)
        totals.append(i)
    total1 = sum(totals)

这适用于程序的其余部分。如果有任何进一步的错误,请毫不犹豫地告诉我并发表评论。

编辑:代码的for i in breakdown部分表示分解中的迭代。

Breakdown是一个数组,因此它存储了值。for循环只是遍历所有这些值,并对每个值执行相同的过程。因此,如果breakdown包含值"1,2,3,4",代码会将该数字转换为整数,然后将其附加到totals数组中。

注意:这部分代码是必要的,因为它可以使sum函数正常工作。List返回一个字符串,而不是整数,因此我们需要将所有值更改为整数

我建议这样做。使用字典将字母映射到值,您可以迭代名称并很容易地获得结果。如果名称中的值与字典中的值不匹配,则它将返回0。

我使用total % 10来获得总数(mod 10),它将始终是个位数。如果总数=10,则这也将得到0。

# dictionary of letter values
letter_dict = {'a': 1, 'j': 1, 's': 1,
              'b': 2, 'k': 2, 't': 2,
              'c': 3, 'l': 3, 'u': 3,
              'd': 4, 'm': 4, 'v': 4,
              'e': 5, 'n': 5, 'w': 5,
              'f': 6, 'o': 6, 'x': 6,
              'g': 7, 'p': 7, 'y': 7,
              'h': 8, 'q': 8, 'z': 8,
              'i': 9, 'r': 9}
# get name
name1 = str(raw_input("Enter your first name and second name "))
# For-loop | get list of values
# Using .get will return the value corresponding to the letter
# If not value matches eg. (' ', !, ' ) then 0 is returned
# .lower is used to include uppercase letters in result
letter_value = []
for s in name1:
    letter_value.append(letter_dict.get(s.lower(), 0))
# While loop
total = sum(letter_value)
while total > 9:
    total = sum(int(n) for n in str(total))
print "your lucky number is " + str(total)

使用列表理解通常是使用for循环将值附加到列表的一种更干净的替代方法。上面的for循环可以用这个替换。

letter_value = [letter_dict.get(s.lower(), 0) for s in name1]

相关内容

最新更新