How to check the serial number in the list?

This is my homework.

The problem is finding a way to check if the items in the list are sequential or not.

Below is the code I wrote:

def consecutive(var):
for x in range(2, len(var)):
    forward = var[x] - var[x-1]
    backward = var[x-1] - var[x-2]
if forward == backward:
    return True
else:
    return False

var = []
print 'Enter your number:'
while True:
    num = raw_input()
    if num == '':
        break
    var += [int(num)]

print consecutive(var)

If I enter numbers like 1, 2, 3, 4, 5 then I will get True

If I enter numbers, for example 2, 6, 3, 9, 7, 1, 4, then I will get False

Here I managed to return True or False values ​​respectively.

But there are two questions that upset me, because if I use my code to resolve issues, I don't get the value I want (this gives me an error)

First question: is an empty list considered a sequential list or not?

Second: a list that includes one value, considered a sequential list, or not?

Do you want to help me?

+4
1

, , , , .

, , (2), . , , return True.

+1

All Articles