In: Computer Science
USING PYTHON
Write a program to create a number list. It will call a function to calculate the average values in the list.
Define main ():
Declare variables and initialize them
Create a list containing numbers (int/float)
Call get_avg function that will return the calculated average values in the list.
Use a for loop to loop through the values in the list and calculate avg
End main()
def get_avg(numbers):
# compute sum
total = 0
for n in numbers:
total += n
# compute average
return total/len(numbers)
def main():
# Declare variables and initialize them
# Create a list containing numbers(int/float)
numbers = [1, 4.6, 65, 12, 8.6, 3]
# Call get_avg function that will return the calculated average values in the list.
avg = get_avg(numbers)
print("Average is: {:.2f}".format(avg))
main()
.
Screenshot:
Output:
.