Question

IN PYTHON Write a function that does not receive parameters but the user must enter numbers...

IN PYTHON
Write a function that does not receive parameters but the user must enter numbers until they give him a 0. While the user does not give him a 0, the program must show how many numbers were entered, the sum of these numbers and the average (not counting the 0)

example:

16
4
42
8
23
15
0

output:

numbres = 6
sum = 108
average = 18.0

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

Please find the code below:

def listenInput():
  
count = 0;
total = 0;
while True:
num = int(input())
if(num==0):
break;
else:
count+=1;
total +=num
  
print("Numbers = ",count)
print("Sum = ",total)
print("Average = ",total/count);
  
if __name__ == '__main__':
listenInput()
  
  
  

output:

Add a comment
Answer #2

You can achieve this in Python by creating a function that takes user input in a loop until the user enters 0. Within the loop, you can keep track of the count, sum, and then calculate the average at the end. Here's a Python function that does that:

pythonCopy codedef calculate_stats():
    count = 0
    total_sum = 0

    while True:
        num = int(input("Enter a number (or 0 to stop): "))        
        if num == 0:            break
        
        count += 1
        total_sum += num    if count == 0:        print("No numbers were entered.")    else:
        average = total_sum / count        print(f"Numbers = {count}")        print(f"Sum = {total_sum}")        print(f"Average = {average:.1f}")

calculate_stats()

When you run this function, it will keep asking the user to enter numbers until they input 0. After the user enters 0, it will display the total count of numbers entered, their sum, and the average (excluding the 0). Using the example you provided, the output will be:

vbnet

Enter a number (or 0 to stop): 16Enter a number (or 0 to stop): 4Enter a number (or 0 to stop): 42Enter a number (or 0 to stop): 8Enter a number (or 0 to stop): 23Enter a number (or 0 to stop): 15Enter a number (or 0 to stop): 0Numbers = 6Sum = 108Average = 18.0


answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
IN PYTHON Write a function that does not receive parameters but the user must enter numbers...
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
  • Program must be in Python 3. Write a program that prompts the user to enter two...

    Program must be in Python 3. Write a program that prompts the user to enter two strings and then displays a message indicating whether or not the first string starts with the second string. Your program must satisfy the following requirements. 1. Your program must include a function called myStartsWith that takes two string parameters. This function returns True if the rst string starts with the second string and returns False otherwise. 2. Your program must include a main function...

  • Using a Python environment, complete two small Python programs - Write a function which repeatedly reads...

    Using a Python environment, complete two small Python programs - Write a function which repeatedly reads numbers until the user enters “done” Once “done” is entered, print out the sum, average, maximum, and minimum of the values entered If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number. 2. Write another function so that it draws a sideways tree pointing right; for example,...

  • Write a program that allows the user to enter a series of exam scores. The number...

    Write a program that allows the user to enter a series of exam scores. The number of scores the user can enter is not fixed; they can enter any number of scores they want. The exam scores can be either integers or floats. Then, once the user has entered all the scores they want, your program will calculate and print the average of those scores. After printing the average, the program should terminate. You need to use a while loop...

  • Write a python program that does the following. a. It asks the user to enter a...

    Write a python program that does the following. a. It asks the user to enter a 5-digit integer value, n. b. The program reads a value entered by the user. If the value is not in the right range, the program should terminate. c. The program calculates and stores the 5 individual digits of n. d. The program outputs a “bar code” made of 5 lines of stars that represent the digits of the number n. For example, the following...

  • Write a Python program (special_sum.py) that calculate a special sum of 4 numbers, all int. The...

    Write a Python program (special_sum.py) that calculate a special sum of 4 numbers, all int. The rule for this special sum is as follow: If one of the entered numbers is 17, then it does not count towards the sum and the numbers entered after it do not count. For full credit, you must use function. Your function should have 4 int as parameters, and returns the special sum (as an int). Call this function specialsum and use main program:...

  • 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 3.6 "While Loops" Write a program that adds up a series of numbers entered by...

    Python 3.6 "While Loops" Write a program that adds up a series of numbers entered by the user. The user should enter each number at the command prompt. The user will indicate that he or she is finished entering the number by entering the number 0.   Example This program will sum a series of numbers. Enter the next number (enter 0 when finished) > 5 Enter the next number (enter 0 when finished) > 2 Enter the next number (enter...

  • Starting out with python 4th edition Write program that lets the user enter in a file...

    Starting out with python 4th edition Write program that lets the user enter in a file name (numbers.txt) to read, keeps a running total of how many numbers are in the file, calculates and prints the average of all the numbers in the file. This must use a while loop that ends when end of file is reached. This program should include FileNotFoundError and ValueError exception handling. Sample output: Enter file name: numbers.txt There were 20 numbers in the file....

  • Objectives Work with functions Assignment Write each of the following functions using Python. The function header MUST b...

    Objectives Work with functions Assignment Write each of the following functions using Python. The function header MUST be written as specified. In your main code test all of the specified functions. Each function must have a comment block explaining what it does, what the parameters are and what the return value is. Please remember the following two guidelines: unless the purpose of the function is to generate output DO NOT write to the screen within the function unless the purpose...

  • In C++ 2. Write a program that allows the user to enter a series of positive...

    In C++ 2. Write a program that allows the user to enter a series of positive numbers between 1 and 100 and displays the smallest and largest of the numbers entered. The program should not store the numbers that were entered, only keep track of the smallest and largest ones. The program should continue accepting numbers until the value 0 is entered and then display the results. If a number out of range is entered, tell the user it is...

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