lundi 23 septembre 2019

How can I get false values when single if statement is true?

Sorry if this is repeated question. This is my first time on StackOverflow. I'm also a beginner with Python.

So, here's the code.

def count_positives_sum_negatives(arr):
    #your code here
  array = [0, 0] #array[0] for sum of positives.  array[1] for sum of negatives.

  for x in arr:
    if x > 0:
      array[0] = array[0] + x
      print(array)



count_positives_sum_negatives([1,2,3,4,-5])

Basically, I want to create an array with count of the sum of positives and sum of negatives. With a given array, it should return [10, -5]. For now, I want to learn and understand something, How can I also get the false value when single if statement is true? I'm thinking of double if-statements or while loop but is this possible with single if statement?
When if statement condition is true, the array becomes [10, 0] so now I have the sum of positives. How should I get sum of negatives -5 which is false value with single if-statement?

Question 2: Why do I get a single repeated value? I'm not using return to stop the loop so I'm confused by this code.

for x in arr:
    while x > 0:
       print(x) # Print 1 again and again...

Aucun commentaire:

Enregistrer un commentaire