以下数字的总和

时间:2013-10-31 14:06:36

标签: python loops for-loop while-loop

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并打印有关它的信息

我的代码出了什么问题?

3 个答案:

答案 0 :(得分: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())

答案 1 :(得分:0)

首先:

digitlist=[]

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

可以简单地替换为:

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

要计算总和,只需调用列表中的sum函数:

sum(digitlist)

答案 2 :(得分:0)

有几个问题:

  1. ...len(digitlis)<sequence_len...,您的变量缺少t
  2. 其次,我不知道你的代码是做什么的,逻辑不是非常直观。
  3. 但是,这是一个简单的程序,可以完成你想要它做的事情,并且我保持尽可能简单:

    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.