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

1 Answer

  1. Here’s a simple code example in Python that meets your requirements:

    
    

    # Initialize a list to hold user inputs

    numbers = []

    # Ask the user for input three times

    for i in range(3):

    number = float(input(f"Enter number {i + 1}: "))

    numbers.append(number)

    # Calculate the average

    average = sum(numbers) / len(numbers)

    # Print the average

    print(f"The average is: {average}")

    ### Explanation:
    1. For Loop: The code uses a for loop to prompt the user to enter three numbers.
    2. User Input: It collects these numbers into a list called `numbers`.
    3. Average Calculation: The average is calculated by summing the list and dividing by the number of elements (3 in this case).
    4. Output: Finally, it prints the average.

    Feel free to try running this code, and if you have any questions or need more assistance, check the extended services page!

Related Questions