0

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!

Murilo
  • 3
  • 3
  • 1
    There is some ambiguities in the code and explanations - why you've to do *float* if you expect integers? For the input part do you expect all 10 numbers entering in one line separated by *space* Or other way? Please take time to make clear first. – Daniel Hao Jul 10 '22 at 13:46
  • 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 – Murilo Jul 10 '22 at 13:48
  • I guess you should save it in a string, converting into a list, splitting by ' ' and then applying logic afterwards – BispensGipsGebis Jul 10 '22 at 13:50
  • 1
    your exercise itself is a bit weird. Why would you "receive 10 integers" (whole numbers), but actually receive floats? – Damiaan Jul 10 '22 at 13:56
  • @Damiaan I know it's strange, but unfortunately it wasn't me who formulated it, but my teacher's weirdness of giving exercises – Murilo Jul 10 '22 at 14:02

2 Answers2

0

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

Damiaan
  • 777
  • 4
  • 11
  • That answer really is much simpler LOL, I guess I like to complicate things a bit. Thanks!!!! it is working fine and it's an easier logic, but it is giving a slightly higher amount of zeros (not sure why, I will take a look) But thanks anyway! – Murilo Jul 10 '22 at 13:58
  • This will not work. Do you forget to convert the input number to *int* first? *TypeError: '>' not supported between instances of 'str' and 'int'* – Daniel Hao Jul 10 '22 at 14:09
0

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
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – lemon Jul 10 '22 at 23:19