2013-10-31 28 views
-2
number = 96154# Replace ??? with a value of your choice. 
sequence_len = 3 # Replace ??? with a value of your choice. 
sum=0 
numbstr=str(number) 
digitlist=[] 

for digit in numbstr: 
digitlist.append(int(digit)) 

while sum!=10 or len(digitlis)<sequence_len: 
sum=0 
if len(digitlist)>=3: 
    for i in range(0,3): 
     sum=sum+digitlist[i] 
    del digitlist [i] 

print sum 

的代码需要检查是否存在以下数字(例如3)的总和是10序列的总和,并打印信息吧总和后面的数字

什么是错误的,我码?

+1

你压痕所有的地方,你能纠正它吗? –

+2

你看到什么错误?可能是'digitlis'的'NameError'? –

+0

不会解决这个问题,但你的'for'循环总和你的数字可以只是'sum = sum(digitlist [:3])' – Hoopdady

回答

0

两个问题:

  1. ...len(digitlis)<sequence_len...,您的变量缺少t。其次,我不知道你的代码是做什么的,逻辑不是非常直观的

但是,这里有一个简单的程序,已经做了你想让它做什么,我一直是尽可能简单:

number = 343703 # Replace ??? with a value of your choice. 
sequence_len = 3 # Replace ??? with a value of your choice. 
numbstr = str(number) 
digitlist = [] 

# Appending all the numbers to a list 
for digit in numbstr: 
    digitlist.append(int(digit)) 

# Looping over all the variables in digitlist, i is the index 
for i, _ in enumerate(digitlist): 
    # If the index, i is 2 less than the length of the list 
    if i < len(digitlist) - 2: 
     # Adding the term and the next two terms after that 
     if digitlist[i] + digitlist[i+1] + digitlist[i+2] == 10: 
      # Printing the list 
      print digitlist[i:i+3] 

Working example.

+0

非常感谢你们! – user2928714

0

for循环后,i将是3.因此del digitlist [i]将删除第3个元素,而不是第1个元素。将其替换为del digitlist [0]。此外,您的while语句条件中的len(digitlis)<sequence_len应为len(digitlis)>=sequence_len。最后,有一个拼写错误; len(digitlis)应该是len(digitlist)

更正代码:

number = 96154# Replace ??? with a value of your choice. 
sequence_len = 3 # Replace ??? with a value of your choice. 
sum=0 
numbstr=str(number) 
digitlist=[] 

for digit in numbstr: 
digitlist.append(int(digit)) 
# len(digitlis)<sequence_len → len(digitlist)>sequence_list 
while sum!=10 or len(digitlist)>sequence_len: # 
    sum=0 
    if len(digitlist)>=3: 
     for i in range(0,3): 
      sum=sum+digitlist[i] 
     del digitlist [0] # del digitlist [i] → del digitlist [0] 

print sum 

一个更紧凑的版本使用Python的特点:

DESIRED_SUM=10 
number = 96154# Replace ??? with a value of your choice. 
sequence_len = 3 # Replace ??? with a value of your choice. 
digit_list = list(map(int,str(number))) 
# Note that if len(digit_list)-sequence_len+1 is negative, the range function will return an empty list, making the generator comprehension empty. any() returns False on an empty iterator (a generator is an iterator). 
indexes = [i for i in range(len(digit_list)-sequence_len+1) if sum(digit_list[i:i+sequence_len])==DESIRED_SUM] 
if len(indexes) > 0: 
    print "{sequence_len} consecutive digits in {number} have a sum of {DESIRED_SUM}.".format(**vars()) 
else: 
    print "No {sequence_len} consecutive digits have a sum of {DESIRED_SUM}.".format(**vars()) 
+0

感谢您的完整代码 - 如何创建一个列表以下数字的总和是10)? – user2928714

+0

非常感谢您的先生,我找到了创建以下数字列表的解决方案,总和为10)再次感谢! – user2928714

+0

@ user2928714'digit_list [i:i + DESIRED_SUM]'如果有帮助,请提高我的答案。如果您认为我的答案是最好的,请在我的答案旁边勾上绿色的勾号,接受我的答案。 –

0

首先:

digitlist=[] 

for digit in numbstr: 
digitlist.append(int(digit)) 

可以通过简单地更换:

digitlist = [int(i) for i in str(number)] 

要计算总和,只需拨打名单上的SUM函数:

sum(digitlist)