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

This program will not work in python 3.6 I can not figure out how to re- type it

ID: 3858525 • Letter: T

Question

This program will not work in python 3.6 I can not figure out how to re- type it so it works in python 3.6

import sys

def readFile(filename):

    f=open(str(filename),'r')

    numbers = [int(num) for num in f]

    return numbers

def getSum(ar):

    sum=0

    for number in ar:

        sum = sum + int(number)

    return sum

def getCount(ar):

    count=0

    for num in ar:

        count = count + 1

    return count

def getAverage(ar):

    avg= float(getSum(ar))/float(getCount(ar))

    return avg

def getMax(ar):

    maximum = ar[0]

    for num in ar:

        if num > maximum:

            maximum = num

    return maximum

def getMin(ar):

    minimum = ar[0]

    for num in ar:

        if num < minimum:

            minimum = num

    return minimum

def getRange(ar):

    maxim = getMax(ar)

    minim = getMin(ar)

    return maxim - minim

def sort(ar):

    length =getCount(ar)

    i = 0

    j = 0

    while i < length:

        j=0

        while j<length -1:

            if ar[j] > ar[j+1]:

                temp = ar[j+1]

                ar[j+1] = ar[j]

                ar[j] = temp

            j=j+1

        i=i+1

def getMedian(ar):

    sort(ar)

    length = getCount(ar)

    if length % 2 is 0 :

        return (ar[length/2 - 1])+ float(ar[length/2])/2.0

    return ar[length/2]

def getMode(ar):

    sort(ar)

    length = getCount(ar)

    maxim = 1

    mode = []

    if length is 1 or 0:

        if length is 1:

            mode.append(str(ar[0]))

            return mode

        else:

            return mode

    i = 0

    count = 1

    while i < length-1:

        if ar[i] != ar[i+1]:

            count=1;

        else:

            count = count + 1

            if count > maxim:

                maxim = count

        i = i + 1

    if ar[i] == ar[i-1]:

        count = count + 1

        if count > maxim:

            maxim = count

    i = 0

    count = 1

    while i < length-1 :

        if ar[i] != ar[i+1]:

            count=1;

        else:

            count = count + 1

            if count == maxim:

                mode.append(str(ar[i]))

        i = i + 1

    if ar[i] == ar[i-1]:

        count = count + 1

        if count == maxim:

            mode.append(str(ar[i]))

    return mode

   

choice = 'y'

while choice is 'y' :

    filename =""

    try:

        filename=input("Enter File Name: ")

   

        array = readFile(filename)

    except IOError:

        print ("Invalid file name ",filename)

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

        continue

    count = getCount(array)

    if count is 0:

        print ("There are no numbers in ",filename)

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

        continue

    sum = getSum(array)

   

    average = getAverage(array)

    print ("Sum :" ,sum)

    print ("Count: ",count)

    print ("Average: ",average)

    print ("Maximum: ", getMax(array))

    print ("Minimum: ", getMin(array))

    print ("Range: ",getRange(array))

    print ("Median: ", getMedian(array))

    mode = getMode(array)

    i=0

    sys.stdout.write('Mode : [')

    for num in mode:

        sys.stdout.write(num)

        if i!=len(mode) -1:

            sys.stdout.write(',')

        i = i + 1

    sys.stdout.write('] ')

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

Explanation / Answer

There were two bugs in your code. One was in readFile method it was not reading file word ny word I changed that mehod. Other bug was in getMedian method where divide operator was wrong.I'm posting working code below with comments:

import sys
def readFile(filename): # readFile method was not working.You should read file word by word
numbers = []
with open(filename,'r') as f:
for line in f:
for word in line.split():
print(word)
numbers.append(int(word))
return numbers
def getSum(ar):
sum=0
for number in ar:
sum = sum + int(number)
return sum
def getCount(ar):
count=0
for num in ar:
count = count + 1
return count

def getAverage(ar):
avg= float(getSum(ar))/float(getCount(ar))
return avg
def getMax(ar):
maximum = ar[0]
for num in ar:
if num > maximum:
maximum = num

return maximum
def getMin(ar):
minimum = ar[0]
for num in ar:
if num < minimum:
minimum = num
return minimum

def getRange(ar):
maxim = getMax(ar)
minim = getMin(ar)
return maxim - minim
def sort(ar):
length =getCount(ar)
i = 0
j = 0
while i < length:
j=0
while j<length -1:
if ar[j] > ar[j+1]:
temp = ar[j+1]
ar[j+1] = ar[j]
ar[j] = temp
j=j+1
i=i+1

def getMedian(ar):
sort(ar)
length = getCount(ar)

if length % 2 is 0 :
return (ar[length//2 - 1])+ float(ar[length//2])/2.0 #return was wrong. Divide operator should be //
return ar[length//2] # same here

def getMode(ar):
sort(ar)
length = getCount(ar)
maxim = 1
mode = []
if length is 1 or 0:
if length is 1:
mode.append(str(ar[0]))
return mode
else:
return mode
i = 0
count = 1
while i < length-1:
if ar[i] != ar[i+1]:
count=1;
else:
count = count + 1
if count > maxim:
maxim = count
i = i + 1
if ar[i] == ar[i-1]:
count = count + 1
if count > maxim:
maxim = count
i = 0
count = 1
while i < length-1 :
if ar[i] != ar[i+1]:
count=1;
else:
count = count + 1
if count == maxim:
mode.append(str(ar[i]))
i = i + 1

if ar[i] == ar[i-1]:
count = count + 1
if count == maxim:
mode.append(str(ar[i]))
return mode

choice = 'y'
while choice is 'y' :
filename =""
try:
filename=input("Enter File Name: ")

array = readFile(filename)
except IOError:
print ("Invalid file name ",filename)
choice = input("Would you like to evaluate another file? (y/n): ")
continue
count = getCount(array)
if count is 0:
print ("There are no numbers in ",filename)
choice = input("Would you like to evaluate another file? (y/n): ")
continue
sum = getSum(array)

average = getAverage(array)
print ("Sum :" ,sum)
print ("Count: ",count)
print ("Average: ",average)
print ("Maximum: ", getMax(array))
print ("Minimum: ", getMin(array))
print ("Range: ",getRange(array))
print ("Median: ", getMedian(array))

mode = getMode(array)
i=0
sys.stdout.write('Mode : [')
for num in mode:
sys.stdout.write(num)
if i!=len(mode) -1:
sys.stdout.write(',')
i = i + 1
sys.stdout.write('] ')
choice = input("Would you like to evaluate another file? (y/n): ")

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote