Averaging a list of user inputed numbers [Python] Pt. 1

User Generated

Fcrapr

Programming

Description

Ok so this is what I'm being asked to do:

Write a program that asks the user to enter some numbers(pos, neg, and zeros).  Do NOT ask to enter a fixed set of numbers or the number of numbers you want to enter.  Ask to enter a few and end with -9999.  The program should create a list with the numbers entered but not to include -9999, then output the list with the following averages:

average of positive numbers, average of negative numbers, average of all the numbers

Current code in Pt. 2

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

list_num =[]

[count_neg,count_pos,count_all] = [0,0,0]

[value_neg,value_pos,value_all]=[0,0,0]

while True:

    value = input("please enter number, if u want to close enter -9999 :")

    if value == -9999:

        break;

    if value <0:

        count_neg = count_neg +1

        value_neg = value_neg+value

    else:

        if value <0:

            count_pos = count_pos +1

            value_pos = value_pos+value

    list_num.append(value)

    count_all = 1+ count_all

print list_num

if count_pos==0:

    avg_pos = 0

else:

    avg_pos = value_pos/count_pos

if count_all==0:

    avg_all= 0

else:

    avg_all = value_all/count_all

if count_neg==0:

    avg_neg = 0

else:

    avg_neg = value_neg/count_neg

print [avg_pos,avg_neg,avg_all]



Anonymous
Great! 10/10 would recommend using Studypool to help you study.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags