Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The code python 3.6 This program reads numbers from a number text file. You can

ID: 3858374 • Letter: T

Question

The code python 3.6

This program reads numbers from a number text file. You can use any number text file and it should work. I can not figure out how to get two modes. The mode when the numbers are even and the mode when the numbers are odd. Like my output gives only Mode: 300---- > but I want it to give me Mode: [300,100] as output just for example.?

def getSum(data):

    total = 0

    for i in data:

        total += i

    return total

def getCount(data):

    return len(data)

def getAverage(data):

    return getSum(data) / getCount(data)

def getMax(data):

    ind = 0

    for i, num in enumerate(data):

        if num > data[ind]:

            ind = i

    return data[ind]

def getMin(data):

    ind = 0

    for i, num in enumerate(data):

        if num < data[ind]:

            ind = i

    return data[ind]

def getRange(data):

    return getMax(data) - getMin(data)

def getMedian(data):

    data = sorted(data)

    size = getCount(data)

    if size % 2 == 1:

        return data[int(size / 2)]

    else:

        return (data[int((size - 1) / 2)] + data[int(size / 2)]) / 2.0

def getMode(data):

    maxCount = 0

    maxValue = 0

    for n1 in data:

        count = 0

        for n2 in data:

            if n1 == n2:

                count += 1

        if count > maxCount:

            maxCount = count

            maxValue = n1

    return maxValue

def readFile(fileName):

    lines = []

    with open(fileName) as f:

        lines = f.readlines()

    return [float(line.strip()) for line in lines]

def main():

    while True:

        fileName = input('What is the file you would like to evaluate? ')

        data = readFile(fileName)

        print('File name: ' + fileName)

        print('Sum: ' + str(getSum(data)))

        print('Count: ', str(getCount(data)))

        print('Average: ', str(getAverage(data)))

        print('Maximum: ', str(getMax(data)))

        print('Minimum: ', str(getMin(data)))

        print('Range', str(getRange(data)))

        print('Median', str(getMedian(data)))

        print('Mode', str(getMode(data)))

        choice = input('Would you like to evaluate another file? (y/n) ')

        if choice == 'n' or choice == 'N':

            break

main()

Explanation / Answer

NOTE: I have modified the mode function separately which you can use in your script. I was not able to use the script given in question because of indentation errors. So separately wrote another program using modified mode function. Below is the script execution on different set of inputs. Please check and let me know if you find any issues. I will revert back within 24 hours.

Code:
http://pasted.co/2cf37888

Execution and output:

Unix Terminal> cat numbers3
5
4
5
8
2
2
1
6
5
1
2
Unix Terminal> python3 mode.py
Enter the filename: numbers3
Mode of given numbers is [5.0, 2.0]
Unix Terminal> cat numbers4
11
15
17
18
19
21
Unix Terminal> python3 mode.py
Enter the filename: numbers4
Mode of given numbers is No Mode
Unix Terminal>