Shallum
Shallum

Reputation: 19

Why is it showing 'None' as an output for my function in python?

Write a function accordian(l) that takes as input a list of integer l and returns True if the absolute difference between each adjacent pair of elements strictly increases.

def accordian(l):

    for i in range(len(l)):
     diff = []
     for i in range(len(diff)):

             diff = l[i] - l[i+1]
             if diff[i] < diff[i+1]:
                     return True
             else:
                     return False

print(accordian([1,3,7,2,9]))

Output: "None"

Upvotes: 0

Views: 101

Answers (2)

Harsha Biyani
Harsha Biyani

Reputation: 7268

You can try:

def accordian(data):
    diff = []
    for index, value in enumerate(data):
        if index == 0:
            continue
        diff.append(data[index] - data[index - 1])

    flag = True
    for index, single_element in enumerate(diff):
        if index == 0:
            continue
        if diff[index] <= diff[index - 1]:
            flag = False
            break
    return flag


print(accordian([1, 3, 7, 2, 9]))
print(accordian([1, 3, 7, 13, 21]))
print(accordian([1, 3, 5, 7, 9]))

Output:

False
True
False

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71560

Your code doesn't work because the inner loop never runs, since the length of diff is 0, however a range of 0 doesn't proceed, your code will work if you add a value to diff, I can't give a code since I don't fully understand what you wanna do.

Upvotes: 1

Related Questions