Saturday, November 15, 2014

Killing some time with python

I was bored again tonight, so I wrote a quick, very simple, Average Calculator in python.
----------------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/python

#Modules to import
import sys

#Little Welcome message
print ("Welcome to avgcalc!\n")

#Get input from the user
user_input = input("Enter a list of numbers separated by a <space>: ")

#Separate those entered numbers by space
numbersent = map(int, user_input.split())

#Generate a list of the numbers
final_list = list(numbersent)

#Count how many numbers entered
num_of_nums = 0
for i in final_list:
    num_of_nums += 1

#Add up all the numbers
total_of_nums = 0
for y in final_list:
    total_of_nums = y + total_of_nums

#Divide the total sum of the numbers by the number of numbers entered to find the average
avg = total_of_nums / num_of_nums

#Print the final result
print ("The average of", final_list, "is", avg)
----------------------------------------------------------------------------------------------------------------------------------------