-1

The point of the following code is to take range of a, b and a list variable and compare the numbers between the range suggested to the list numbers.

If the number is on the list greater than a and lower than b, count it as one. And none, that count nothing.

def between(lst, a, b):
    i=len(lst)
    count = 0
    for n in range(a, b):
        if lst[n] > a and lst[n] < b:
            count = count + 1
        else:
            count = count + 0                   
    return count
    
# TESTS 

print("********************")
print("Starting the test:")
    
print("********************")
print("Counting over [1, 2, 3, 4] with a = 0, b = 5")
ans = between([1, 2, 3, 4], 0, 5)
if ans == 4:
    print("CORRECT: Very good, there are 4 elements between a = 0 and b = 5 in [1, 2, 3, 4]")
else:
    print("WRONG: There are 4 elements between a = 0 and b = 5 in [1, 2, 3, 4] but the code returned", ans)

print("********************")
print("Counting over [-5, 20, 13, 0] with a = 200, b = 300")
ans = between([-5, 20, 13, 0], 200, 300)
if ans == 0:
    print("CORRECT: Very good, there are no elements between a = 200 and b = 300 in [-5, 20, 13, 0]")
else:
    print("WRONG: There are no elements between a = 200 and b = 300 in [-5, 20, 13, 0] but the code returned", ans)
    
print("********************")
print("Counting over [-5, 20, 13, 0] with a = -10, b = 5")
ans = between([-5, 20, 13, 0], -10, 5)
if ans == 2:
    print("CORRECT: Very good, there are 2 elements between a = -10 and b = 5 in [-5, 20, 13, 0]")
else:
    print("WRONG: There are 2 elements between a = -10 and b = 5 in [-5, 20, 13, 0] but the code returned", ans)

print("********************")    
print("Tests concluded, add more tests of your own below!")
print("********************")
mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

You have an error in your between function. You could have found it using a debugger (see this question for example).

A correct version :

def between(lst, a, b):
    count = 0
    for elem in lst:
        if a <= elem <= b:
            count = count + 1                 
    return count

A shorter (and faster) version :

def between(lst, a, b):
    return sum(1 for elem in lst if a <= elem <= b)
Lenormju
  • 4,078
  • 2
  • 8
  • 22