0

I am creating a python program, to allow a user to input numbers and find the total, average, highest and lowest of the numbers inputted. my teacher has told me to improve the program by removing the first and last number inputted into the list.I am confused on how doing so,as the list does not exist until the user has inputted the numbers into it.

THIS IS MY CODE SO FAR:

totalList=[]
totalList = totalList[1:-1]
TotalNum = int(input("Please enter how many numbers are to be entered:"))
Change=TotalNum
Input=0
Total=0
while TotalNum>0:
    TotalNum = TotalNum - 1
    Input = int(input("Enter Number: "))
    totalList.insert(-1,Input)
    Total = Total +Input
print("Total:" ,Total)
print("Average:",Total/Change)
print("Highest:",max(totalList))
print("Lowest:",min(totalList))
None
  • 3
  • 2
  • Don't do `.insert(-1,Input)`, that's almost certainly not what you want. Use `.append(Input)` – juanpa.arrivillaga Mar 13 '17 at 18:38
  • "*as the list does not exist until the user has inputted the numbers into it.*". And what stops you from putting the total calculation outside the loop and [removing](http://stackoverflow.com/questions/627435/how-to-remove-an-element-from-a-list-by-index-in-python) the first and last elements from the list before that? – dhke Mar 13 '17 at 18:40
  • Anyway, I don't really understand your question. Surely your teacher means to remove the first and last numbers *after* the user has inputted them. – juanpa.arrivillaga Mar 13 '17 at 18:40
  • sorry,that is is what i meant to say. – None Mar 13 '17 at 18:42
  • Simplest solution is to have 2 loops. 1 to input numbers and one to do calculations with `totalList = totalList[1:-1]` in between. – 001 Mar 13 '17 at 18:56

0 Answers0