Search code examples
pythonvalidationwhile-loopconditional-statementscontrol-structure

How to make simple number identification Python?


I have this exercise:

  1. Receive 10 integers using input (one at a time).
  2. Tells how many numbers are positive, negative and how many are equal to zero. Print the number of positive numbers on one line, negative numbers on the next, and zeros on the next.

That i need to solve it with control/repetition structures and without lists. And i'm stuck in the first part in dealing with the loops

Until now I only writed the part that deal with the amount of zeros, I'm stuck here:

n = float(input())  #my input

#Amounts of:

pos = 0   # positive numbers
neg = 0   # negative numbers
zero = 0  # zero numbers

while (n<0):
    resto = (n % 2)  
    if (n == 0):    #to determine amount of zeros
        zz = zero+1
        print (zz)  
    elif (resto == 0): #to determine amout of positive numbers
        pp = pos+1
        print (pp)
    elif (n<0):    #to determine amount of negative numbers
        nn = neg+1
    else:
        ("finished")

My inputs are very random but there are like negatives and a bunch of zeros too and obviously some positive ones. What specific condition i write inside while to make it work and to make a loop passing by all the numbers inside a range of negative and positive ones?

Soo.. i made it turn into float because theres some broken numbers like 2.5 and the inputs are separated by space, individual inputs of numbers one after other

example input (one individual input at a time):

25
2.1
-19
5
0 
 # ------------------------------------------
 #  the correct awnser for the input would be:
3                 #(amount of Positive numbers)
1                  #(amount of Negatives numbers)              
1                 #(amount of Zeros numbers)
how to make them all pass by my filters and count each specific type of it?

obs: i can't use lists!


Solution

  • Why not something like this?

    pos = 0
    neg = 0
    zer = 0
    for x in range(10):
        number = int(input())
        if number > 0:
            pos +=1
        if number < 0:
            neg +=1
        else: # number is not positive and not negative, hence zero
            zer +=1
    print(pos)
    print(neg)
    print(zer)
    

    EDIT: Thanks @Daniel Hao for pointing out that casting to int is necessary with input().