I made this simple program with functions to calculate mean, median, and mode from a list of numbers. I want to select all the decimal numbers, whatever they are (10.00034, 1, $5.46, $0.90 or 0.5 for example from a string something like "5, 7 83 100, $5.07, and 7.834") from a string and cast them to float. How can I do that in Python 3? I want to capture all decimal numbers with leading zeros as floats including their leading zeros.
import string
def mode(x):
if len(x) == 0:
return 0
else:
theDictionary = {}
for number in x:
count = theDictionary.get(number, None)
if count == None:
theDictionary[number] = 1
else:
theDictionary[number] = count + 1
theMaximum = max(theDictionary.values())
for key in theDictionary:
if theDictionary[key] == theMaximum:
print("Mode: ", key)
return key
def median(x):
if len(x) == 0:
return 0
else:
x.sort()
midpoint = len(x) // 2
print("Median: ", end=" ")
if len(x) % 2 == 1:
print(x[midpoint])
med = x[midpoint]
else:
print((x[midpoint] + x[midpoint - 1]) / 2)
med = (x[midpoint] + x[midpoint - 1]) / 2
return med
def mean(x):
if len(x) == 0:
return 0
else:
theSum = 0
theSum = float(theSum)
for number in x:
theSum = theSum + number
average = theSum / len(x)
print("Mean: ", average)
return average
def main():
lyst = []
print ("Enter a list of numbers or enter to quit: ")
numbers = input()
if numbers == "":
return 0
else:
lyst = numbers.split()
lyst = [float(x) for x in lyst]
print("List: ", lyst)
mode(lyst)
median(lyst)
mean(lyst)
main()
if __name__ == "__main__":
main()
I tried a few things I found here on stackoverflow and they haven't worked. Any help would be appreciated.