Question

In: Computer Science

Write a python program that will ask the user to enter as many positive and negative...

Write a python program that will ask the user to enter as many positive and negative numbers and this process will only stop when the last number entered is -999. Use a while loop for this purpose. Your program should compute three sums; the sum of all numbers, sum of all positive numbers, and the sum of all negative numbers and display them. It should also display the count of all numbers, count of all positive numbers, and count of all negative numbers. Finally, it should compute and display the average of all numbers, average of all positive numbers, and average of all negative numbers.

Solutions

Expert Solution

Python program for the above question:


tot_sum=0
pos_sum=0
neg_sum=0

tot_cnt=0
pos_cnt=0
neg_cnt=0

while(1):
        x= int(input('Enter a number or enter -999 to exit:'))
        if(x==-999):
                if(tot_cnt!=0):
                        tot_avg = tot_sum / tot_cnt
                else:
                        tot_avg= "Not defined as total count is zero."

                if(pos_cnt!=0): 
                        pos_avg = pos_sum / pos_cnt
                else:
                        pos_avg = "Not defined as postive count is zero."

                if(neg_cnt!=0): 
                        neg_avg = neg_sum / neg_cnt
                else:
                        neg_avg = "Not defined as negative count is zero."

                print("The sum of all the numbers is: ",tot_sum)
                print("The sum of all positive numbers is: ",pos_sum)
                print("The sum of all negative numbers is: ",neg_sum)

                print("The count of all the numbers is: ",tot_cnt)
                print("The count of all positive numbers is: ",pos_cnt)
                print("The count of all negative numbers is: ",neg_cnt)

                print("The average of all the numbers is: ",tot_avg)
                print("The average of all positive numbers is: ",pos_avg)
                print("The average of all negative numbers is: ",neg_avg)

                exit(0)


        else:
                tot_sum= tot_sum + x
                tot_cnt=tot_cnt+1

                if(x>0):
                        pos_cnt=pos_cnt+1
                        pos_sum=pos_sum+x
                
                if(x<0):
                        neg_cnt=neg_cnt+1
                        neg_sum=neg_sum+x

EXPLANATION:

I have taken the help of a while loop with true condition and whenever the input value is -999 then it prints all the details and exit. If the number is other than -999 then i am checking for whether it is positive or negative and adding to that sum ,increasing the respective counts and finally counting the average, I have include not defined statements so to avoid division by zero.

NOTE: In case of any doubt/problem regarding any part of the code or explanation please feel free to let me know in the comment section so that i can solve your doubt/problem as soon as possible and after reading the solution kindly, please UPVOTE.


Related Solutions

Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The program will use a while loop to let the program keep running. The user will be allowed to use the program as long as the quitVariable is equal to 'y' or 'Y'. When the user decides to quit playing, say "Thanks for playing!". The program will use a for loop to test if the number are even or odd. If even print "number is...
Write a python program that will ask the user to enter any number of days. The...
Write a python program that will ask the user to enter any number of days. The program reads the number of days from the user, and returns how many years, months, and days are there in the given number of days. Your program should prompt the user to: Enter the number of days : 1152 The user types number of days (1152 above, for example) and hit enter key. The program then will print: 3 years 1 months 27 days...
Make a program that lets the user enter a positive or negative integer as many times...
Make a program that lets the user enter a positive or negative integer as many times as they want. java //import statement //class header //Begin class scope //Method header for main. //Begin method scope. //Declare input object for Scanner. //Declare variable called entry initialized to zero. //Declare variable called response initialized to blank space. //Prompt "Do you want to enter an integer? Y or N: " //Store value in correct variable.   //while header that tests uppercase in response //Begin scope...
Write, save, and run a Python program Ask the user to enter a number of grams....
Write, save, and run a Python program Ask the user to enter a number of grams. Your program converts that to whole tones, then whole kilograms and whatever is left is in grams. It prints the entered grams , tones , kilograms and the remaining grams.      4 marks for i in range(0,10): print(i) 2    what is wrong in the above python code? display your explanation using print statement. mark 3    insert the following comments into your program of part...
Write a Python program to: ask the user to enter two integers: int1 and int2. The...
Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative number,...
Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT