Question

A script in Python 2.7 Create a script that prints the average that continuously updates as...

A script in Python 2.7

Create a script that prints the average that continuously updates as new numbers are entered in python.

Sample Run

Welcome to the Average Calculator, please insert a number
New number 1
The current average is 1.0
New number 3
The current average is 2.0
New number 6
The current average is 3.333
New number 6
The current average is 4.0
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code :

________________________________________________________

print("Welcome to the Average Calculator, please insert a number")
sum = 0
count = 0
while 1 :
    n = input()
    print("New number " + str(n) )
    sum = sum + n
    count = count + 1
    average = sum / float(count)
    print("The current average is " + str(average) )

_________________________________________________

Explanation :

A while loop is used to take new numbers as input and count average each time a new number comes. sum is a global variable which stores sum of all numbers input till now and count stores the count of numbers.

_________________________________________________

Input :

______________________________________

Output :

Spython main.py Welcome to the Average Calculator, please insert a number New number 1 The current average is 1.0 New number

____________________________________________

Please comment in case you have any doubt or anything is not clear.

Add a comment
Know the answer?
Add Answer to:
A script in Python 2.7 Create a script that prints the average that continuously updates as...
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
  • Exercise 1 - Create a List to Sum Values Write a python script that sums the...

    Exercise 1 - Create a List to Sum Values Write a python script that sums the values in a list called add_list that contains the following values: (10, 2, -4, 8). Make sure you include a for loop when iterating through the list to sum the values. Take screenshots of the code and output. Exercise 2 – Create a Dictionary Write a python script that prints a dictionary where the key numbers are 1, 2, 3, 4, and 5 and...

  • The create_python_script function creates a new python script in the current working directory, adds the line...

    The create_python_script function creates a new python script in the current working directory, adds the line of comments to it declared by the 'comments' variable, and returns the size of the new file. Fill in the gaps to create a script called "program.py". 1- def create_python_script(filename): 2 comments = "# Start of a new Python program" with open("filename", "w+") as file: file.write(comments) import os filesize - os.path.getsize(filename) print("The size of the file is:") return(filesize) Reset 3 4 5 6 7...

  • I need help with this python programming exercise, please! thanks in advance Create a Python script...

    I need help with this python programming exercise, please! thanks in advance Create a Python script file called hw4.py. Add your name at the top as a comment, along with the class name and date. Both exercises should be in this file, with a comment before each of them to mark it. Ex. 1. Write a program that inputs an integer number from the user, then prints a letter "O" in ASCII art using a width of 5 and the...

  • Write a Python (3) program to simulate** a buffer overflow (this is a new script, not...

    Write a Python (3) program to simulate** a buffer overflow (this is a new script, not part of the intranet assignment), then implement input validation to prevent it. There are two parts to this assignment: 1) The program should display a welcome message and prompt the user for a username. Create a simulated buffer overflow condition by allowing a user to input more data than the size of the allocated memory (causing the program to crash). 2) Implement input validation...

  • Python Script format please! 1. Write a script that takes in three integer numbers from the...

    Python Script format please! 1. Write a script that takes in three integer numbers from the user, calculates, and displays the sum, average, product, smallest, and largest of the numbers input. Important things to note: a. You cannot use the min() or max() functions, you must provide the logic yourself b. The calculated average must be displayed as an integer value (ex. If the sum of the three values is 7, the average displayed should be 2, not 2.3333). Example:...

  • You will create a shell script that prints out the following information about the system it is run on: • The system’s n...

    You will create a shell script that prints out the following information about the system it is run on: • The system’s name and IP address • How much memory the system is currently using, and how much total memory it has • The number of CPUs of the machine • The number of unique users on the system at that moment When your script is run, each of these values should be clearly labeled in the output, such as...

  • Write a python [Eng ver.] Create a program that prints the largest number, a decimal number,...

    Write a python [Eng ver.] Create a program that prints the largest number, a decimal number, among the N numbers you enter. (Of the N numbers, a decimal number must be included at least one.) [Example] >N: 5 Enter a number : 1 Enter a number : 5 Enter a number : 78 Enter a number : 4 Enter a number : 3 Output : 5

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • Python: Firstly, create a function and all the work is to be completed as 1 function...

    Python: Firstly, create a function and all the work is to be completed as 1 function inside the function: a loop to run 3 times generating a random number from 1-10 add those 3 numbers together. if the number is greater than or equal 25, print out, the numbers are large numbers else if the number is less than or equal to 5, the sum of the numbers is small and default, the number is average

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