Home > other >  How to make simple number identification Python?
How to make simple number identification Python?

Time:07-11

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!

CodePudding user response:

If I understood you right, I think the below code may be what you are after. Wishing you much success in your future ventures and projects!:D

pos_count = 0
neg_count = 0
zero_count = 0
turn_count = 0

while turn_count < 10:
    user_input = input("Please enter a whole number: ") #Get input from user

    try:
        user_number = int(user_input) # Catch user input error
    except:
        print("Input not valid. Please enter a whole number using digits 0-9")
        continue # Start over

    # Check each number:
    if user_number > 0:
        pos_count  = 1
        turn_count  =1
    elif user_number < 0:
        neg_count  = 1
        turn_count  =1
    elif user_number == 0:
        zero_count  = 1
        turn_count  =1


print("Positive count:", pos_count) # Print positive count
print("Negative count:", neg_count) # Print negative count
print("Zero count:", zero_count) # Print zero count

CodePudding user response:

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().

  • Related