2/07/2022

python testEqual function

 testEqual compares what is returned by the distance function and the 0 (the correct answer).


import test

def distance(x1, y1, x2, y2):

    dx = x2 - x1

    dy = y2 - y1

    dsquared = dx**2 + dy**2

    result = dsquared**0.5

    return result


test.testEqual(distance(1,2, 1,2), 0)

test.testEqual(distance(1,2, 4,6), 5)

test.testEqual(distance(0,0, 1,1), 1.41421)




Pass
Pass
Pass

2/02/2022

python list range not printing the last one, last one need to be larger than the one you want to print

 For even numbers we want to start at 0 and count by 2’s. So if we wanted the first 10 even numbers we would use range(0,19,2). The most general form of the range is range(start, beyondLast, step).


print(list(range(0, 20, 2)))

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Not printing 20 as you would thought

The randrange function generates an integer between its lower and upper
argument, using the same semantics as range — so the lower bound is included, but
the upper bound is excluded.

diceThrow = random.randrange(1, 7)       
# return an int, one of 1,2,3,4,5,6 But not 7