I need this list to append a list for each line in a file, then to sort the output. It takes input from a file (numbers.txt). I've tried everything I can think of to get it to sort; finally I've made some 'progress' but newlist2 here only returns none and newlist isn't sorted. What am I doing wrong?
import sys
import os
print("\n && ************************************************ &&\n && Welcome to the text document average calculator! && \n && ************************************************ &&\n\n Please ensure your file contains only numeric charactesr! \n By default, the program will look for a document named 'numbers' in this folder.")
def averager():
####Declarations
count = int(0)
sunn = int(0)
target = str('')
beginbool = bool(False)
####Statement
try:
while beginbool == False:
specify = input(" Specify directory? Y / N: ")
if any(specify.lower() == f for f in ['y', 'Y', 'Yes', 'YES', 'YEs', 'yES', 'yEs', 'yeS']):
rngfile = open(input('Please input file location: '), 'r')
beginbool == True
break
elif any(specify.lower() == f for f in ['N', 'n', 'No', 'no', 'nO', 'NO']):
rngfile = open("numbers.txt", 'r')
beginbool == True
break
else:
print('Please input yes, no, or something close at least...')
pass
####User Data
print('\n&&***********************************&&')
print("&& Line: LiVal: Sum: &&\n&&***********************************&&")
####For loop
for line in rngfile:
iteration = int(line)
truelist = list([])
truelist.append(int(iteration))
count = 1
sunn = iteration
if sunn == 0 and count == 0:
print("Empty list detected.")
elif sunn != 0 and count != 0:
newlist = [int(x) for x in truelist]
newlist.sort(key=int, reverse=True)
newlist2 = newlist.sort(key=int, reverse=True)
print(sorted(newlist))
print(newlist2)
pass
else:
pass
####Close
rngfile.close()
####Exceptions
except IOError:
print("Error reading the file.")
except ZeroDivisionError:
print("Divided by zero? (This usually happens when the filename is incorrect, or the file is empty.)")
####Display Average
average = (sunn / count)
####User Data
print('&&***********************************&& \n\n The numerical average of the file is: ', average)
print(' Thank you for using the numerical averager!')
####Run
averager()
####Finish
input("\n Press any key to exit.")
sys.exit(1)
CodePudding user response:
.sort()
method is sorting list in-place, it always returns None
so in the line:
newlist2 = newlist.sort(key=int, reverse=True)
You are assign None to the newlist2 variable, consider using sorted
function.
CodePudding user response:
You wrote
for line in rngfile:
...
truelist = list([])
truelist.append(int(iteration))
That doesn't look right. You ensure it has zero lines, then you append a line.
Didn't you want to initialize truelist
before
starting the for
loop?
You wrote
newlist.sort(key=int, reverse=True)
newlist2 = newlist.sort(key=int, reverse=True)
Again, that doesn't look right.
The .sort()
return value will always be None
.
I recommend you avoid .sort()
, and prefer sorted()
.
It is more modern and offers a far more
convenient interface, handing back a new list each time.
It supports the same options, e.g. key=
and reverse=
.