Quizzma Latest Questions

How can you calculate the average of three numbers using a for loop?

Your code should use a for loop.
Your code should ask the user for input.
With input 90, 30, and 30, the average should be 50.
With input 200, 800, and 200, the average should be 400.




Leave an answer

Leave an answer

2 Answers

  1. To create a simple program that calculates the average of three user inputs using a for loop, you can follow these steps:

    1. Ask the user for the three numbers they want to average.
    2. Use a for loop to accumulate the sum of the numbers.
    3. Calculate the average by dividing the total sum by the number of inputs (3).
    4. Print the average.

    Here is a Python code snippet that demonstrates this:

    
    

    # Initialize sum variable

    total_sum = 0

    # Ask for input three times

    for i in range(3):

    # Get user input

    number = float(input("Enter a number: "))

    # Add the number to total_sum

    total_sum += number

    # Calculate the average

    average = total_sum / 3

    # Print the average

    print("The average is:", average)

    ### Explanation:

    – The loop runs three times (as specified by `range(3)`), allowing the user to input three different numbers.

    – Each time, the program adds the input number to `total_sum`.

    – Finally, it calculates the average by dividing `total_sum` by 3 and prints the result.

    Using this code, if you input the numbers 90, 30, and 30, the average will be 50. Similarly, for inputs 200, 800, and 200, the average will be 400 as required. If you need more assistance, feel free to check the extended services

  2. # Initialize the total to 0
    total = 0

    # Use a for loop to take 3 scores as input
    for i in range(3):
    score = int(input(“Scores: “))
    total += score # Add each score to the total

    # Calculate the average
    average = total / 3

    # Print the average score
    print(“Average score: ” + str(average))

Related Questions