-5
def main():
i = int( input ("Enter an interger, the input ends if it is 0: "))
count_pos = 0
count_neg = 0
total = 0
if (i != 0):
    while (i != 0):
        if (i > 0):
            count_pos += 1
        elif (i < 0):
            count_neg += 1
        total += i
    i = int( input ("Enter an interger, the input ends if it is 0: "))
    count = count_pos + count_neg

print ("The number of positives is", count_pos)
print ("The number of negatives is", count_neg)
print ("The total is", total)

File "", line 16 print ("The number of positives is", count_pos) ^ IndentationError: unindent does not match any outer indentation level

2 Answers2

0

See let's take a list , say sample_list

sample_list = [1, 5, -9, 6, 7, -7, -2, -4, 2, 3]

Or

sample_list = []
while True:
    a = int(raw_input())
    if a==0: break
    else:sample_list.append(a)

Now, To get the length of list

sample_list_length = len(sample_list)

where len() is a inbuilt function which returns the length of any iterable object like strings,lists,etc.

positive_number = 0
negative_number = 0
for dummy_number in sample_list:
    if dummy_number>=0:
        positive_number+=1
    else:
        negative_number+=1

print "There are",positive_number,"positive numbers"
print "There are",negative_number,"negative numbers"
print "There are",sample_list_length,"total numbers"
ZdaR
  • 22,343
  • 7
  • 66
  • 87
0

There are a couple of problems with your code.

First of all,

while (i != 0):
    if (i > 0):
        count_pos += 1
    elif (i < 0):
        count_neg += 1
    total += i

is an infinite loop. i never changes, so if i!=0 is True the first time, then it'll always be True.

Next,

i = int( raw_input ("Enter an interger, the input ends if it is 0: "))
count = count_pos + count_neg

are completely unnecessary. Neither i not count is ever used in the following code.


The fixed code:

def main():
    count_pos = 0
    count_neg = 0
    while True:
        i = int( input ("Enter an interger, the input ends if it is 0: "))
        if i > 0:
            count_pos += 1
        elif i < 0:
            count_neg += 1
        else:
            break

    print ("The number of positives is", count_pos)
    print ("The number of negatives is", count_neg)
    print ("The total is", count_pos+count_neg)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149