简体   繁体   中英

Python loops with break/continue

Given a set of integers numbers = [10, 0, 11, 5, 3, 0, 6, 0, 2, 0, 6, 9] containing at least two zeros. Print the sum of numbers from the given set located between the last two zeros (if the last zeros are in a row, then print 0). My attempt:

numbers = [10, 0, 11, 5, 3, 0, 6, 0, 2, 0, 6, 9]
index_of_first_zero = 0
i = 0
while i < len(numbers):
    if numbers[i] == 0:
        index_first_zero = 1
        break
    i += 1

index_of_last_zero = len(numbers) - 1
i = len(numbers) - 1
while i >= 0:
    if numbers[i] == 0:
        index_of_last_zero = i
        break
    i -= 1


summa = 0
while i in range(index_of_first_zero+1, index_of_last_zero):
    summa += numbers[i]

print("Summa =", summa)

But unput is Summa = 0 Can you help me please?

It's much easier to reverse the list and look for the first two zeros.

>>> numbers = [10, 0, 11, 5, 3, 0, 6, 0, 2, 0, 6, 9]
>>> numbers_rev = reversed(numbers)
>>> sum_ = 0
>>>
>>> for x in numbers_rev:
...    if x == 0:
...        break
>>>        
>>> for x in numbers_rev:
...    if x == 0:
...        break
...    sum_ += x
>>>    
>>> sum_
2

Alternative:

numbers = [10, 0, 11, 5, 3, 0, 6, 0, 2, 5, 6, 0, 6, 9]
numbers_rev = numbers[::-1]
sum_ = 0

for x in numbers_rev[numbers_rev.index(0)+1:]:
    if x == 0:
        break
    sum_ += x   

There are a few mistakes in your program...

One mistake in your program is that in the final part you're telling the script: "While the variable i is in the iterator range do [...]"

However, you should put a for loop there, not a while, changing it to:

summa = 0
for i in range(index_of_first_zero+1, index_of_last_zero):
    summa += numbers[i]

print("Summa = ", summa)

Now that part of the program should work properly, because i in the for loop will be replaced with the values in the range iterator.

Instead, with the while , i takes index_of_last_zero as value, so it will never be in the range iterator.

However, the second error is a logical one: you have to sum the numbers between the last two zeros, not between the first and the last one, so the best thing to do is to reverse the list as other users already answered, so the entire program has to change:

original_list = [10, 0, 11, 5, 3, 0, 6, 0, 2, 0, 6, 9]
reversed_list = reversed(original_list)
my_sum = 0

for num in reversed_list:
  if num == 0: 
    # it breaks here because it found the first zero, 
    # and then it will continue the cycle from the next element
    break 

# Now it won't loop again from the beginning, but from where it broke before.
for num in reversed_list:
  if num == 0:
    break
  my_sum += num

print(my_sum) # -> 2

This program will work, thanks to @timgeb, and works with reversed() which is a built-in function that returns an Iterator object.

Here will be clarified to you what is an Iterator and how to work with it.

Anyway, I'll put here another solution that won't use that function.

original_list = [10, 0, 11, 5, 3, 0, 6, 0, 2, 0, 6, 9]
reversed_list = [] # we'll create it manually
my_sum = 0

# let's create here the reversed_list
for i in range(len(original_list) - 1, 0, -1):
  reversed_list.append(original_list[i])

while i < len(reversed_list):
  if reversed_list[i] == 0: 
    # it breaks here because it found the first zero, 
    # and we store the index of the first zero.
    index_first_zero = i
    break 

  i += 1

# Now it won't loop again from the beginning, but from where it broke before.
for i in range(index_first_zero, len(reversed_list)):
  if reversed_list[i] == 0:
    break
  my_sum += reversed_list[i]

print(my_sum) # -> 2

This should do the trick...

a = [10, 0, 11, 5, 3, 0, 6, 0, 2, 0, 6, 9]
i1 = a[::-1].index(0)
i2 = a[::-1].index(0,i1+1)
print("Summa =",sum(a[len(a)-i2:len(a)-i1]))

I hope this makes it clear :)

#take your original list
numbers = [10, 0, 11, 5, 3, 0, 6, 0, 2, 0, 6, 9]
#reverse the list
numbes = numbers.reverse()

#make clear that the first zero has not been encountered
encountered_zero = False
#set the temporary sum to 0
cur_sum = 0
#for every number in your list
for number in numbers:
#if it's a zero, and you haven't passed any yet
    if number == 0 and not encountered_zero:
        #mark it
        encountered_zero = True
        #skip the rest of the iteration
        continue
    #if you have already encountered a zero
    if encountered_zero == True:
        #add every number to the sum
        cur_sum += number
    #if you encounter another zero
    if encountered_zero == True and number == 0:
        #break out of the loop, you're done
        break
#here you have your answer
summa = cur_sum 

print("Summa =", summa)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM