简体   繁体   中英

my python doesn't return 3,4,5,6,7 it seems that it doesn't work

    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?

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.

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]

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM