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

时间:2015-11-28 14:57:07

标签: python-3.x

该程序假设要求您输入全名并将其转换为数字,然后它将继续在列表中添加数字,直到获得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循环,不断添加每个数字,直到其左边有1位数字(我需要帮助)

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!"

2 个答案:

答案 0 :(得分:0)

我建议这样做。使用字典将字母映射到值,您可以遍历名称并轻松获得结果。如果名称中的值与字典中的值不匹配,则它将返回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]

答案 1 :(得分:0)

以下代码解决了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”,则代码会将该数字转换为整数,然后将其附加到总数数组。

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