def removal(DataList, n):
if len(DataList) < 2*n:
print "Not enough elements!"
for i in range(n):
DataList.remove(min(DataList))
DataList.remove(max(DataList))
return sorted(DataList)
def main():
inputs = []
while True:
inp = raw_input("Please enter a new integer, press ENTER if you want to stop: ")
if inp == "":
break
else:
inputs.append(inp)
removal(inputs,2)
if __name__=='__main__':
main()
main()
I got a ValueError: min() arg is an empty sequence
At a meanwhile, if I do not write my code in the main() and if name=='main', I'd be fine with the following code.
def removal(DataList, n):
#return to a sorted list with n smallest and n largest numbers removed.
for i in range(n):
DataList.remove(min(DataList))
DataList.remove(max(DataList))
return sorted(DataList)
inputs = []
while True:
inp = raw_input("Please enter a new integer, press ENTER if you want to stop: ")
if inp == "":
break
else:
inputs.append(inp)
removal(inputs,2)
Could somebody explain why it doesn't work in former and how should I make it work?
With:
if __name__=='__main__':
main()
main()
You're calling the main
function twice (when your program is run as the main program), so after you enter a series of numbers and press another Enter to end the input, main
calls removal
to do the intended operations, which do not produce any output, and returns to the main program to start another call to main
, which prompts for another series of number input. If at this point you simply press another Enter, you would end up with an empty list for the inputs
list, which your removal
function does not handle properly and would call min
with the empty list, causing the said exception of ValueError: min() arg is an empty sequence
.