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"))
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
for j in range(5):
print(j-1)
L = [4, "ahoy", 5]
for elem in L:
print(elem)
for i in range(len(L)):
print(L[i])
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)
L = [4, "ahoy", 5]
s = "["
for i in range(len(L)-1):
s += (str(L[i]) + ", ")
s += str(L[-1])
s += "]"
#print every other element of the list
L = [5, 4, 10, 2, 3, 43, 12, 30]
for e in L[::2]:
print(e)
for i in range(0, len(L), 2):
print(L[i])
for i in range(len(L)//2):
print(L[2*i])
i = 0
while i < len(L):
print(L[i])
i += 2
i = 0
while i <= len(L): #should be i < len(L)
print(L[i])
i += 1
for i in range(5):
for j in range(3):
for k in range(2):
print(i, j, k)
for i in range(5):
for j in range(3):
for k in range(2):
print(k, j, i)
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)
#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
print(contains_5([4, 3, 10, 5, 6, 7]))
print(contains_5([4, 3, 10, 6, 7, 10]))
#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])
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¶#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]