Question

I really need help with this python programming assignment Program Requirements For part 2, i need...

I really need help with this python programming assignment

Program Requirements

For part 2, i need the following functions

• get floats(): It take a single integer argument and returns

a list of floats. where it was something like this

def get_floats(n):

  lst = []

  for i in range(1,n+1):

    val = float(input('Enter float '+str(i)+': '))

    lst.append(val)

  return lst

• summer(): This non-void function takes a single list argument, and returns the sum of the

list. However, it does not use the sum() function to do so. See "Summing Values the Hard

Way" below for instructions on how to implement this function.

• average(): This non-void function uses the list of numbers returned by get floats() as

its argument and returns the average value, a float, to the calling program. It calls summer()

to sum the values in the list. Its body can actually be a single line: a return statement followed

by the appropriate expression. See the notes below on "Calculating Averages."

• std dev(): This non-void function takes two arguments, the list of numbers obtained by

get floats() and the average obtained using average(), and returns the standard deviation,

a float value. The function should initialize an accumulator to zero and then use a

for-loop in which the average is subtracted from the list value and the result is squared and

added to the accumulator. After the for-loop has finished looping, the accumulator should be

divided by the number of elements in the list, the square root taken, and the result returned to

the calling function. See the notes below on "Calculating Standard Deviations."

• main(): Finally, write the void function main(). It should prompt a user for the number

of elements desired in the list and should call the non-void functions get floats(),

average(), and std dev(). Then it should print the average and standard deviation to the

screen. Use the round() function to round off the standard deviation to two decimal places.

Summing Values the HardWay1

summer() takes a single list argument. First you need to initialize an accumulator to zero, then

use a for-loop to add the value of each element in the list, and finally return the total sum to the

calling program. The proper behavior of summer() is demonstrated below.

1The easy way is to use the built-in function sum().

5

1 >>> x = [9, 3, 21, 15]

2 >>> summer(x)

3 48

4 >>> summer([31.12, -16.32, 24.9, 82.0, 14.0])

5 135.7

Calculating Averages

Recall that the average of a list of numbers is simply the sum of the values divided by the number of

values. Mathematically, for a set of numbers x1, x2, · · ·, xN, the average is given by

¯x =

1

N

(x1 + x2 + · · · + xN)

where N is the number of terms in the list. The average, or mean, is often indicated by an overbar—

i.e., ¯x indicates the average of a set of numbers xk. The average is also given by

¯x =

1

N

XN

k=1

xk

where the uppercase Greek letter Sigma () means perform a sum. The expression to the right of

Sigma is what is to be summed. The expression below Sigma initializes a summation index (in this

case k) and the term above Sigma indicates the final value of the summation index.

Calculating Standard Deviations

The standard deviation is a measure of how much fluctuation or deviation there is in a set of numbers

after the average has been subtracted from each of the values2. If all the values in a list are equal to

the average, then the standard deviation is zero. If the standard deviation is large, then it means many

values deviate considerably from the average. The standard deviation is represented by the lowercase

Greek letter sigma (). For a set of numbers x1, x2, · · ·, xN, the standard deviation is given by

=

s

1

N

[(x1 − ¯x)2 + (x2 − ¯x)2 + · · · + (xN − ¯x)2]

where ¯x is the average. Recall that in Python we can square a value z using z ** 2 and we can

calculate the square root using z ** 0.5.

The following three examples demonstrate the results you should obtain for three different sets of

input for your stats.py program. Each of these has the same mean but an increasing standard

deviation. In the first, all the values are the same and hence the standard deviation is zero.

1 Enter the number of list elements: 5

2 Enter float 1: 50

2For further discussion of standard deviation, see <http://en.wikipedia.org/wiki/Standard_deviation>.

6

3 Enter float 2: 50

4 Enter float 3: 50

5 Enter float 4: 50

6 Enter float 5: 50

7 Average = 50.0

8 Standard deviation = 0.0

Here the values are spread out, spanning the range 30 to 70:

1 Enter the number of list elements: 5

2 Enter float 1: 30

3 Enter float 2: 40

4 Enter float 3: 50

5 Enter float 4: 60

6 Enter float 5: 70

7 Average = 50.0

8 Standard deviation = 14.14

In the final example the values range between 10 and 90:

1 Enter the number of list elements: 5

2 Enter float 1: 10

3 Enter float 2: 30

4 Enter float 3: 50

5 Enter float 4: 70

6 Enter float 5: 90

7 Average = 50.0

8 Standard deviation = 28.28

Don't forget to include docstrings in all your functions!

please check & show indentations

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

Code:

import math

def get_floats(n):

lst = []

for i in range(1,n+1):

val = float(input('Enter float '+str(i)+': '))

lst.append(val)

return lst

def summer(l):

s = 0

for n in l:

s += n

return s

def average(l):

return summer(l)/len(l)

def stddev(l, avg):

std = 0

for v in l:

std += (v-avg)*(v-avg)

std = std/(len(l))

return math.sqrt(std)

def main():

n = int(input("Enter the number of list elements: "))

l = get_floats(n)

avg = average(l)

std_dev = stddev(l, avg)

print("Average = ",avg)

print("Standard deviation = %.2f"%std_dev)

main()

Add a comment
Know the answer?
Add Answer to:
I really need help with this python programming assignment Program Requirements For part 2, i need...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...

    Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std; /* * Calculate the square of a number */ float square (float x) { return x*x; } /* * Calculate the average from a list of floating point numbers */ float average(vector<float>& values) { float sum = 0.0f; float average; for (float x : values) sum += x; average = sum / values.size(); return average; } /** Calculate the standard deviation from a vector...

  • What I need: Write a program named Averages that includes a method named Average that accepts...

    What I need: Write a program named Averages that includes a method named Average that accepts any number of numeric parameters, displays them, and displays their average. For example, if 7 and 4 were passed to the method, the ouput would be: 7 4 -- Average is 5.5 Test your function in your Main(). Tests will be run against Average() to determine that it works correctly when passed one, two, or three numbers, or an array of numbers. What I...

  • Change your C++ average temperatures program to: In a non range-based loop Prompt the user for...

    Change your C++ average temperatures program to: In a non range-based loop Prompt the user for 5 temperatures. Store them in an array called temperatures. Use a Range-Based loop to find the average. Print the average to the console using two decimals of precision. Do not use any magic numbers. #include<iostream> using namespace std; void averageTemperature() // function definition starts here {    float avg; // variable declaration    int num,sum=0,count=0; /* 'count' is the int accumulator for number of...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • I really need help with this python programming assignment asap please double check indentations, thank you...

    I really need help with this python programming assignment asap please double check indentations, thank you We were unable to transcribe this imageEnter a drink option: Milk 22 Guest #1: a Please select the number of your entree choice 1) Planked salmon, 2) Prime rib, 3) Sesame tofu with spinach: 3 Please select the number of your dessert choice 27 1) Caramel oat bar, 2) Chocolate decadence, 3) Orange creme brulee: 1 Please select the number of your drink choice...

  • I need help with this in C++ Program 2) Display and sum up all the numbers...

    I need help with this in C++ Program 2) Display and sum up all the numbers of factor of 5 in a given range. a) Request a number range (min/max values) separated by a space. b) Use a validation loop to ensure that both max and min are integers and max is greater than min. c) Output all the numbers of factor of 5 between the max and min. d) Output the sum of all the displayed numbers. e) Output...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • DQuestion 19 28 pts Problem 2. Quadratic Equations A quadratic equation has the form: a bc0 The two solutions are given by the formula: 2a Write a program with a loop that a) solves quadratic equatio...

    DQuestion 19 28 pts Problem 2. Quadratic Equations A quadratic equation has the form: a bc0 The two solutions are given by the formula: 2a Write a program with a loop that a) solves quadratic equations with coefficients read from the terminal, b) visualizes the corresponding quadratic function az2 brc0using the matplotlib module. Instructions Write all code for this problem in a file p2.py 1. The program consists of a main whtle loop (an infinite loop) in which the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT