Home>

The bottom line is that you need to create a list from the interval n to the interval x , as here:

x= int (input ("End:"))
for i in range (n, x + 1):
    print (i)
# Start: 2
# End: 6
# 2
# 3
# 4
# 5
# 6

But if I try to do the same, but with a list

a= []
n= list (input ("Start:"))
x= list (input ("End:"))
for i in range (n, x):
    print (a [i])

then throws an error

Traceback (most recent call last):
    for i in range (n, x):
TypeError: 'list' object cannot be interpreted as an integer

The end result should look like:

a= []
n= list (input ("Start:")) # 2
x= list (input ("End:")) # 6
for i in range (n, x + 1):
    print (a [i])
# [2,3,4,5,6]

The range arguments can only be numbers, no need to try to cram lists into them. What was wrong with the working int (input (?

andreymal2021-09-29 20:06:13

In general, the whole problem is solved in one line a= list (range (int (input ("Start:")), int (input ("End:")) + 1))

andreymal2021-09-29 20:06:13
  • Answer # 1

    I like this:

    a= [_ for _ in range (int (input ("Start:")), int (input ("End:")) + 1)]
    print (a)
    

    Input example:

    Start: 5

    End: 10

    Sample output:

    [5, 6, 7, 8, 9, 10]

    please try to leave a little more detailed answers. you can complete the answer by clicking edit

    aleksandr barakin2021-09-29 05:31:52

    Using _ in this case is not very appropriate. This is usually the name of variables with which there is no further interaction, and you use its value to generate a list.

    zaborschicov2021-09-29 10:43:11
  • Answer # 2

    Can be shorter

    a= [i for i in range (int (input ("Start:")), int (input ("End:")) + 1)]
    
  • Answer # 3

    missedappend:

    a= []
    n= int (input ("Start:")) # 2
    x= int (input ("End:")) # 6
    for i in range (n, x + 1):
        a.append (i)
    print (a)
    # [2,3,4,5,6]