login code, again

In [1]:
def login(username, password):
    '''Return True of you can log in
    Lock everyone out after three unsuccessful logins in a row
    '''
    global conseq_failed_logins
    global usernames, passwords
    
    #Three situtation where the login is unsuccesful:
    # * username is not in usernames
    # * the password that corresponds to username is not password
    # * the login was disable
    
    if conseq_failed_logins == 3:
        #conseq_failed_logins += 1
        return False
    
    if username not in usernames:
        conseq_failed_logins += 1
        return False
        
    #I know for sure that username is in usernames if I'm executing 
    #code below this text
    
    if passwords[usernames.index(username)] != password:
        conseq_failed_logins += 1
        return False
    else:
        conseq_failed_logins = 0
        return True
    
    

def initialize():
    global usernames, passwords, conseq_failed_logins
    usernames = ["ab12", "cd23", "df12"]
    passwords = ["abcd", "defg", "hijk"]
    
    #The number of consecutive failed login attempts
    #(not updated after it's set to 3)
    conseq_failed_logins = 0
    #for all i, the password for usernames[i] is passwords[i]
        
initialize()

if __name__ == '__main__':        
    
    print(login("ab12", "abcd"))
    print(login("ab11", "abcd"))
    
    print(login("ab12", "abcd"))
    print(login("ab11", "abcd"))
    
    print(login("ab12", "abcd"))
    print(login("ab11", "abcd"))
    
    print(login("ab12", "abcd"))
True
False
True
False
True
False
True

Return in functions

In [2]:
def f():
    print("hi")
    if False:
        return 5
    print("hello")
    return "ahoy"
    
if __name__ == '__main__':
    print(f())
    #f()
    
    
    
#if <cond> == True:
#    #do stuff
#else:
#    #do other stuff
hi
hello
ahoy

For and while

In [3]:
for j in range(5):
    print(j-1)
-1
0
1
2
3
In [4]:
L = [4, "ahoy", 5]
for elem in L:
    print(elem)
4
ahoy
5
In [5]:
for i in range(len(L)):
    print(L[i])    
4
ahoy
5
In [6]:
L = [3, 2, 10, 4]
s = 0
for blah in L:
    s += blah    
    print("s is now", s)
print("The sum of L is", s)    
s is now 3
s is now 5
s is now 15
s is now 19
The sum of L is 19
In [7]:
L = [4, "ahoy", 5]
s = "["
for i in range(len(L)-1):
    s += (str(L[i]) + ", ")

s += str(L[-1])
s += "]"
In [8]:
#print every other element of the list

L = [5, 4, 10, 2, 3, 43, 12, 30]
for e in L[::2]:
    print(e)
5
10
3
12
In [9]:
for i in range(0, len(L), 2):
    print(L[i])
5
10
3
12
In [10]:
for i in range(len(L)//2):
    print(L[2*i])   
5
10
3
12
In [11]:
i = 0
while i < len(L):
    print(L[i])
    i += 2    
5
10
3
12
In [12]:
i = 0
while i <= len(L): #should be i < len(L)
    print(L[i])
    i += 1    
5
4
10
2
3
43
12
30
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-12-9dc663169a2f> in <module>()
      1 i = 0
      2 while i <= len(L): #should be i < len(L)
----> 3     print(L[i])
      4     i += 1

IndexError: list index out of range

Nested loops

In [13]:
for i in range(5):
    for j in range(3):
        for k in range(2):
            print(i, j, k)
0 0 0
0 0 1
0 1 0
0 1 1
0 2 0
0 2 1
1 0 0
1 0 1
1 1 0
1 1 1
1 2 0
1 2 1
2 0 0
2 0 1
2 1 0
2 1 1
2 2 0
2 2 1
3 0 0
3 0 1
3 1 0
3 1 1
3 2 0
3 2 1
4 0 0
4 0 1
4 1 0
4 1 1
4 2 0
4 2 1
In [14]:
for i in range(5):
    for j in range(3):
        for k in range(2):
            print(k, j, i)  
0 0 0
1 0 0
0 1 0
1 1 0
0 2 0
1 2 0
0 0 1
1 0 1
0 1 1
1 1 1
0 2 1
1 2 1
0 0 2
1 0 2
0 1 2
1 1 2
0 2 2
1 2 2
0 0 3
1 0 3
0 1 3
1 1 3
0 2 3
1 2 3
0 0 4
1 0 4
0 1 4
1 1 4
0 2 4
1 2 4

Lab 6 partial solution

In [15]:
import random


def print_board_and_legend(board):
    for i in range(3):
        line1 = " " +  board[i][0] + " | " + board[i][1] + " | " +  board[i][2]
        line2 = "  " + str(3*i+1)  + " | " + str(3*i+2)  + " | " +  str(3*i+3) 
        print(line1 + " "*5 + line2)
        if i < 2:
            print("---+---+---" + " "*5 + "---+---+---")
        
    
    
def make_empty_board():
    board = []
    for i in range(3):
        board.append([" "]*3)
    return board
    

def board_is_full(board):    #doesnt contain empty space
    '''Return True iff board is full
    (board[i][j] != " " for all combinations of i, j)
    '''
    for i in range(len(board)):
        for j in range(len(board[0])):
            print("Checking if " + str([i, j]) + " is empty")
            if board[i][j] == " ":
                return False
                
    return True
    
def num_to_coord(num):
    return [(num-1)//3, (num-1) % 3]

def put_in_board(board, mark, square_num):
    coord = num_to_coord(square_num)
    board[coord[0]][coord[1]] = mark
    
if __name__ == '__main___':
    board = make_empty_board()
    

    print_board_and_legend(board)    
    move = int(input("Your move: "))
    put_in_board(board, "X", move)
    while not board_is_full(board):

        print_board_and_legend(board)    
        move = int(input("Your move: "))
        put_in_board(board, "O", move)
        
        print_board_and_legend(board)    
        move = int(input("Your move: "))
        put_in_board(board, "X", move)
        
###########################################################
    mark = "X"
    while not board_is_full(board):

        print_board_and_legend(board)    
        move = int(input("Your move: "))
        put_in_board(board, mark, move)
        if mark == "X":
            mark = "O"
        else:
            mark = "X"
    
    # print("\n\n")
    # 
    # board = [["O", "X", "X"],
    #          [" ", "X", " "],
    #          [" ", "O", " "]]
    # print_board_and_legend(board)    

   ##   print(board_is_full(board))
    # 
    #print_board_and_legend(board)            

Contains 5

In [16]:
#does the list L contain the number 5

def contains_5(L):
    for elem in L:
        print("Looking at the element: ", elem)
        if  elem == 5:
            return True
    
    return False
In [17]:
print(contains_5([4, 3, 10, 5, 6, 7]))
Looking at the element:  4
Looking at the element:  3
Looking at the element:  10
Looking at the element:  5
True
In [18]:
print(contains_5([4, 3, 10,  6, 7, 10]))
Looking at the element:  4
Looking at the element:  3
Looking at the element:  10
Looking at the element:  6
Looking at the element:  7
Looking at the element:  10
False
In [19]:
#print XOXOXOXOXO

mark = "X"
for i in range(100):
    print(mark)
    if mark == "X":
        mark = "O"
    else:
        mark = "X"
        
L = ["X", "O"]

#L[0], L[1], L[0], L[1], L[0], L[1], ...

for i in range(10):
    print(L[i%2])        
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O
X
O

Diagonals

In [20]:
def is_right_diag_all_marks(board, mark):
    for i in range(3):
        if board[i][2-i] != mark:
            return False
            
    return True
    

is_seq_all_marks

In [21]:
#direction (0, 1)

#(0, 0) -> (0, 0) + 1*(0, 1) -> (0, 0) + 2*(0, 1)

#direction (1, 0)

#left-diag (1, 1)

#right-diag (1, -1)

def is_seq_all_marks(board, start, direction, mark):
    point = start[:]
    for i in range(3):
        if board[point[0]][point[1]] != mark:
            return False
        point[0] += direction[0]
        point[1] += direction[1]