-1

In the following code, I am unable to accept the input list values from console.

    s=[]
for i in range(10):
    s[i]=int(input('enter integers from 1 to 10\n'))


mini=11
for temp in s:
    if mini>temp:
            mini=temp
print('minimum : '+str(mini))

maxi=0
for temp in s :
    if maxi<temp:
        maxi=temp
print('maximum :'+str(maxi))

IndexError : list argument index out of range.

Cant find where index went out of range.Please help, thanks in advance.

Vikranth Inti
  • 111
  • 1
  • 2
  • 8
  • Your error has nothing to do with the title (a homework?). It is ok to ask about specific issues with your code and you should *mention the context* but *don't put it in the title*. You could change the title: "IndexError while reading a list of integers". Provide only relevant parts of the code (drop everything after the error line); include the *full* traceback. Please, limit your questions to one issue per question (you could include links to related questions for context). – jfs Jul 16 '15 at 11:28
  • I am a novice programmer in Python.its not homework, but making trail and error method for learning to code sir, I will make a note of your points definitely. – Vikranth Inti Jul 16 '15 at 11:31

2 Answers2

3

You should be appending, you cannot index an empty list so s[i] will fail straight away with s[0] as the list is empty:

s = []
for i in range(10):
   s.append(int(input('enter integers from 1 to 10\n')))

mini,maxi = 0, 11
for temp in s:
    if temp < mini:
        mini = temp
    if temp > maxi:
        maxi = temp
print('minimum : '+str(mini))
print('maximum :'+str(maxi))

You can also check the two in a single loop as above instead of doing two iterations over s.

You can also use a list compt to create your list of numbers:

s = [int(input('enter integers from 1 to 10\n')) for _ in range(10)]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

you should append to the list.

s=[]
for i in range(10):
    s.append(int(input('enter integers from 1 to 10\n')))


mini=11
for temp in s:
    if mini>temp:
        mini=temp
print('minimum : '+str(mini))

maxi=0
for temp in s :
    if maxi<temp:
        maxi=temp
print('maximum :'+str(maxi))
omri_saadon
  • 10,193
  • 7
  • 33
  • 58