1
    def number_range(num1,num2):
         list1=[]
        for i in range(num2-num1):
            num1 = num1+1
            list1.append(num1)
        return list1


 number_range(2,8)

I want python to return 3,4,5,6,7 but my code just does not work.could anyone help me?

  • 3
    I'm curious why you don't just use list(range(3, 8)) – Sri May 03 '20 at 15:34
  • what's wrong with `range(num1, num2)` or `[num for num in range(3, 8)]`? –  May 03 '20 at 15:35
  • Does this answer your question? [What is the difference between Python's list methods append and extend?](https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend) – Joe May 03 '20 at 16:17
  • https://stackoverflow.com/questions/41452819/list-append-in-for-loop – Joe May 03 '20 at 16:18
  • 1
    @Fatemeh Jamali what is the output – sh1hab May 03 '20 at 16:35
  • @Joe no, those have nothing to do with the problem. This is a simple logical error, or else a failure to understand the semantics of `range`. Voted to close as a typo. – Karl Knechtel Sep 15 '22 at 02:19

4 Answers4

0

What you're doing is simply calling a function, but not printing its results out to the console.

def a():
    return 1
a()

will do nothing. If you want the results out to the console, then you need to call

print(a())

to actually get the results.

Kevin Sheng
  • 411
  • 1
  • 5
  • 8
0

if range only have 1 parameter it will always start from 0. Your code will generate numbers from 0 to 8 - 2, or 6

>>> range(8 - 2) == range(6)
True
>>> range(8-2)
range(0, 6)
>>> list(range(6))
[0, 1, 2, 3, 4, 5]

What you want is starting from 2 and ending at 8. You have to use 2 parameters, one for the starting point, the other for the end

>>> range(3, 8)
[3, 4, 5, 6, 7]

in your example

def number_range(num1,num2):
    list1=[]
    for i in range(num1+1, num2):
        list1.append(i)
    return list1

Then you can call your function and print the output

print(number_range(2,8))

And you will see in your terminal

[3, 4, 5, 6, 7]
Lwi
  • 354
  • 4
  • 10
0

If you want your code to print the output from the function call, you'll have to change the last line with this:

print(number_range(2,8))

Also, your function's output for that input will be [3, 4, 5, 6, 7, 8]. As they've told you, the way you used range() is not common at all and could be changed by the standard (first, last+1).

If you REALLY want to use range() that way, which is basically a range() with a single parameter, you'd have to do it like this, substracting 1:

def number_range(num1,num2):
list1=[]
for i in range(num2-num1-1):
    num1 = num1+1
    list1.append(num1)
return list1


print(number_range(2,8))
[3, 4, 5, 6, 7]

In any case, while I was writing this answer Shazers provided an implementation using standard syntax, and I highly recommend his solution over mine. Use this one only if you were using range() with a substraction for a certain reason you didn't comment on.

0
def number_range(num1,num2):
    list1=[]
    for i in range(num2-num1):
        num1 = num1+1
        list1.append(num1)
    return list1


print(number_range(2,8))

The above snippet gives you [3, 4, 5, 6, 7, 8] but you wish to see [3, 4, 5, 6, 7]. May be you need to check once syntax of range(start,end,step) that could solve your problem.

def number_range(num1,num2):
    list1=[]
    diff = num2-num1
    end = diff+1
    for i in range(num1, diff+1):
        num1 = num1+1
        list1.append(num1)
    return list1


print(number_range(2,8))

Output

[3, 4, 5, 6, 7]

If you wish to do something different please provide us more about your requirements so that it can be modified in that way.

Shakeel
  • 1,869
  • 15
  • 23