However when i run the code it gives me this error: File \"/Users/../Desktop/pro
ID: 3586756 • Letter: H
Question
However when i run the code it gives me this error:
File "/Users/../Desktop/proj1/exaple.py lint 88, in ChooseOrdering
if abs(sum(ascending) - sum(descending)) < 0.00001:
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
PYTHON
def OrderCurriences(fp, fp_out=sys.stdout):
ascending = []
descending = []
for line in fp:
line = line.replace("," , "")
l = []
copylist = []
l.append(line) # appends list into array
copylist.append(line) # appends list into copyarray (same array that comes from file)
m = mergeSort(l)
ascending.append(m)
#inversion counts gets appended into the array each time a line goes through
n = DSort(copylist)
descending.append(n)
ChooseOrdering(ascending,descending,fp_out=sys.stdout) #returns inversion counts to function
pass
def main():
fp = open(input("Insert Filename (with extension) for input: ").strip())
fp_out = open(input("Insert Filename (with extension) for output: ").strip(), 'w')
OrderCurriences(fp, fp_out)
fp_out.close()
# OrderCurriences(fp)
# fp.close()
main()
def ChooseOrdering(ascending, descending, fp_out=sys.stdout)
if abs(sum(ascending) - sum(descending)) < 0.00001:
outcome = "TIE"
else:
outcome = "ASCENDING" if sum(ascending) < sum(descending) else "DESCENDING"
# directing output to file
print("Total Number of portfolio's assessed: ", len(ascending), file=fp_out)
print(" " * 12, "{:^12s} vs {:^12s}".format("Ascending", "Descending"), file=fp_out)
for pairs in range(len(ascending)):
print("Portfolio #{:02d} {:^12d} vs {:^12d}"
.format(pairs + 1, ascending[pairs], descending[pairs]), file=fp_out)
print(" Total Inversion Count", file=fp_out)
print("Ascending: ", sum(ascending), " vs. Descending: ", sum(descending), file=fp_out)
print(" Best Form of Ordering: ", outcome, file=fp_out)
File:
testcase00.txt
1,2,3
0,0,0
2,3,2,1,5
5,4,3,2,1
HELP IM NOT SURE WHAT IS WRONG
Explanation / Answer
Hi,
I went through your code line by line.
What does error say:
if abs(sum(ascending) - sum(descending)) < 0.00001:
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
You are trying to add an Integer value with NoneType. It means the problem is with your array either ascending or descending. NoneType is the type for None object, which means No Value.
I assume you have your own sorting functions for MergeSort and Dsort or Some third party libraries. Please check whether your Sorting function returns proper value. Without those sorting functions, it could not be proceed further.
To Make sure that function returns proper value:
After each sort function in the loop, Add a print statement as follows
print(m)
print(n)
If you get a No value data from your sorting function, you can verify it.
If you are not able to figure out the error, Please provide me your sorting functions. We can solve this.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.