• 2
  • 167 Views
  • All levels

Write a program in python that reads an unspecified number of integers from the user, determines how many positive and negative values have been read, computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point with 2 digits after the decimal. Here is an example of sample run: Enter an integer, the input ends if it is 0: 1 Enter an integer, the input ends if it is 0: 2 Enter an integer, the input ends if it is 0: -1 Enter an integer, the input ends if it is 0: 3 Enter an integer, the input ends if it is 0: 0 The number of positivies is 3 The number of negatives is 1 The total is 5 The average is 1.25

Leave an answer

Our People Answers

0

(Based on todays review)

  • frknkrtrn

    The program which displays the number of positive and negative values inputted. The program written in python 3 goes thus :

    pos_val = 0

    #initialize a variable to hold the number of positive values

    neg_val = 0

    #initialize a variable to hold the number of negative values

    sum = 0

    #initialize a variable to hold the sum of all values

    while True :

    #A while loop is initiated

    val = int(input("Enter integer values except 0 : "))

    #prompts user for inputs

    if val == 0 :

    #checks if inputted value is 0

     break

    #if True end the program

    else :

    #otherwise

     sum +=val

    #add the value to the sum variable

     

     if val > 0 :

    #checks if value is greater than 0

      pos_val += 1

    #if true increase positive value counts by 1

     else :

      neg_val += 1

    #if otherwise, increase negative value counts by 1

    freq = float(pos_val + neg_val )

    #calculates total number of values

    print('The number of positive values is = ', pos_val)

    #display number of positive values

    print('The number of negative values is = ', neg_val)

    #display number of negative values

    print('The total sum of values is = ', sum)#display the total sum

    print('The average is {:.2f}' .format((sum))

    #display the average of the inputs

    Learn more : brainly.com/question/19323897

  • fichoh


    Answer:

    count_p = 0

    count_n = 0

    total = 0

    while True:

       number = int(input("Enter an integer, the input ends if it is 0: "))

       if number == 0:

           break

       else:

           total += number

           if number > 0:

               count_p += 1

           elif number < 0:

               count_n += 1

    print("The number of positives is: " + str(count_p))

    print("The number of negatives is: " + str(count_n))

    print("The total is: " + str(total))

    print("The average is: " + str(total / (count_p + count_n)))

    Explanation:

    Initialize the variables, count_p represens the number of positives, count_n represents the number of negatives, and total represents the total of the numbers

    Create a while loop iterates until the user enters 0. If the number is not 0, then add it to the total. If the number is greater than 0, increase count_p by 1. If the number is smaller than 0, increase count_n by 1.

    When the loop is done, print the count_p, count_n, total, and average