1
given_list5=[7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total7=0
i = 0
while True:
    if given_list5[i] >=0:
        break
    total7 += given_list5[i]
    i += 1
print(total7)

This is my code. Please fix it for me. Thanks.

Holly Hao
  • 11
  • 1

4 Answers4

1
given_list5=[7, 5, 4, 4, 3, 1, -2, -3, -5, -7]

total_neg=0

for i in given_list5:    #that means you select every item in the list by one by. it's a loop
    if i<0:
        total_neg+=i     #that means total_neg=total_neg + i
print(total_neg)
bibakcamki
  • 83
  • 7
1

Python allows single-line for loop convention. Also, for something like this, you can simply do:

given_list5 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]

filtered_list5 = [i for i in given_list5 if i < 0]
total7 = sum(filtered_list5)
print(total7)
>> -17

Explanation: filtered_list5 filters the list of only negative numbers, and sum() computes sum of all the elements in a list.

Combining everything in a single line: total7 = sum([i for i in given_list5 if i < 0])

EDIT Looking at the code-style by OP, as mentioned by @Nick, here is an implementation using:

a) for-loop

given_list5 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]

total7 = 0
for number in given_list5:
    if number < 0:
        total7 += number
        
print(total7)
>> -17

b) while-loop is actually tricky, if you are a beginner. To loop a list using while loop, you will have to use an inbuilt function .pop() on the list. You can check check this explanation. So, on your code, the implementation would be:

given_list5 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]

total7 = 0
while given_list5:
    number = given_list5.pop()
    if number < 0:
        total7 += number
        
print(total7)
>> -17
Mr. Hobo
  • 530
  • 1
  • 7
  • 22
  • I think I have mentioned that `[i for i in given_list5 if i < 0]` part of the code is _single-line_ for loop convention in python :) – Mr. Hobo Dec 12 '20 at 07:10
  • Check the thread on [single line for-loop](https://stackoverflow.com/questions/1545050/python-one-line-for-expression) with python! kudos :) – Mr. Hobo Dec 12 '20 at 07:11
  • Well, I guess that's fair enough but I don't think it's what OP meant (especially given their code). – Nick Dec 12 '20 at 07:17
  • 1
    True, but I still don't understand the `break`. Best guess, OP has just started python and want to use all the functionalities! (Looks like a homework question anyway) – Mr. Hobo Dec 12 '20 at 07:36
1

for num in given_list5 : loop given_list5 elements

str.isdigit() (check for numbers) : True = str, False : numeric

given_list5=[7, 5, 4, 4, 3, 1, -2, -3, -5, -7]

sumNegativeQuantity = 0

for num in given_list5 :
    if (str(num).isdigit() == False) and (num < 0) :
        sumNegativeQuantity += num  

print(sumNegativeQuantity)

result

-17
Journey R
  • 51
  • 2
1
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total6 = 0
for element in given_list3:
    if element >= 0:
        continue
    total6 += element
print(total6)
>> -17
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Orexy
  • 11
  • 1