In [1]:
# login() returns True if the user can log in, False o/w
# lock everyone out if there are three consecutive failed
# login attempts


#passwords[users.index(user)]


def login(user, password):
    global n_failed_attempts
    
    if n_failed_attempts >= 3:
        return False

    #I know that n_failed_attempts < 3
    
    if user not in users:
        n_failed_attempts += 1
        if n_failed_attempts == 3:
            print("You're locked out")
        
        return False
    
    if passwords[users.index(user)] == password:
        n_failed_attempts = 0
        return True
    else:
        n_failed_attempts += 1
        if n_failed_attempts == 3:
            print("You're locked out")
        
        return False
        





def initialize():
    global users, passwords, n_failed_attempts
    users = ["a", "b", "c"]
    passwords = ["x", "y", "z"]
    #users[i], passwords[i]
    n_failed_attempts = 0
    
initialize()
if __name__ == '__main__':
    initialize()
   #Test case 1: does logging in work with correct password?
    if login("a", "x") == True:
        print("Test 1 passed")
    else:
        print("Test 1 failed")
        
    initialize()
    #Test case 2: does logging not work with incorrect password?     
    if login("a", "z") == False:
        print("Test 2 passed")
    else:
        print("Test 2 failed")
    
    initialize()
    #Test case 3: does logging not work with incorrect password?     
    if login("z", "a") == False:
        print("Test 3 passed")
    else:
        print("Test 3 failed")
Test 1 passed
Test 2 passed
Test 3 passed
In [2]:
import math
def approx_pi(n_terms):
    s = 0
    for i in range(n_terms):
        s += ((-1)**i)/(2*i+1)
    return 4*s
    
#how many terms are needed to approximate to within n significant digits?

def agree_to_ndigits(a, b, n):
    '''return True if 
    a and b agree to n sig. digits
    '''    
    a *= 10**(n-1)
    b *= 10**(n-1)
    return int(a) == int(b)
    
n = 1
n_digits = 5
while not agree_to_ndigits(math.pi, approx_pi(n), n_digits):
    n += 1

def approx_pi_to_ndigits(n_terms, n_digits):
    '''return the number terms needed to get
    the approximation correct within n_digits'''
    s = 0
    for i in range(n_terms):
        s += ((-1)**i)/(2*i+1)
        if agree_to_ndigits(4*s, math.pi, n_digits):
            return (i+1)
    return 4*s

print(approx_pi_to_ndigits(100000000000, 5))


def approx_pi_to_ndigits(n_terms, n_digits):
    '''return the number terms needed to get
    the approximation correct within n_digits'''
    s = 0
    i = 0
    while not agree_to_ndigits(4*s, math.pi, n_digits):
        s += ((-1)**i)/(2*i+1)
        i += 1
    return i

print(approx_pi_to_ndigits(100000000000, 5))


print(n)
10794
10794
10794