from random import randint
board = []
dimensions = 0
while dimensions < 4:
if dimensions < 4:
print("Please try again")
dimensions = int(input("How many rows and columns do you want? Must be greater than or equal to 4 "))
for x in range(0, dimensions):
board.append(["?"] * dimensions)
def print_board(board):
for row in board:
print(" ".join(row))
num = randint(0, 1)
if num == 0:
direction = "ltr"
else:
direction = "utd"
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
def create_ship(board):
ship_coords = []
ship_row = random_row(board)
ship_col = random_col(board)
if direction == "ltr":
if ship_col + 1 in range(len(board)) and ship_col - 1 in range(len(board)):
ship_coords.append([ship_row, ship_col + 1])
ship_coords.append([ship_row, ship_col])
ship_coords.append([ship_row, ship_col - 1])
elif ship_col + 1 in range(len(board)) and ship_col + 2 in range(len(board)):
ship_coords.append([ship_row, ship_col + 1])
ship_coords.append([ship_row, ship_col])
ship_coords.append([ship_row, ship_col + 2])
elif ship_col - 1 in range(len(board)) and ship_col - 2 in range(len(board)):
ship_coords.append([ship_row, ship_col - 1])
ship_coords.append([ship_row, ship_col])
ship_coords.append([ship_row, ship_col - 2])
elif direction == "utd":
if ship_row + 1 in range(len(board)) and ship_row - 1 in range(len(board)):
ship_coords.append([ship_row + 1, ship_col])
ship_coords.append([ship_row, ship_col])
ship_coords.append([ship_row - 1, ship_col])
elif ship_row + 1 in range(len(board)) and ship_row + 2 in range(len(board)):
ship_coords.append([ship_row + 1, ship_col])
ship_coords.append([ship_row, ship_col])
ship_coords.append([ship_row + 2, ship_col])
elif ship_row - 1 in range(len(board)) and ship_row - 2 in range(len(board)):
ship_coords.append([ship_row - 1, ship_col])
ship_coords.append([ship_row, ship_col])
ship_coords.append([ship_row - 2, ship_col])
return ship_coords
def get_rows(ship_coords):
rows = []
for indexs in ship_coords:
rows.append(int(indexs[0]))
return rows
def get_cols(ship_coords):
cols = []
for indexs in ship_coords:
cols.append(int(indexs[1]))
return cols
def calculate_turns(difficulty):
if difficulty.lower() == "easy":
return int(dimensions * dimensions / 2)
if difficulty.lower() == "medium":
return int(dimensions * dimensions / 4)
if difficulty.lower() == "hard":
return int(dimensions * dimensions / 6)
if difficulty.lower() == "impossible":
return 1
print("Please try again")
return 0
turns = 0
while turns == 0:
turns = calculate_turns(input("Type how hard do you want the difficulty to be ('easy', 'medium', 'hard', 'impossible') "))
print("You have", turns, "tries to destroy my battleship!")
print_board(board)
ship = create_ship(board)
ship_rows = get_rows(ship)
ship_cols = get_cols(ship)
#print(ship_rows)
#print(ship_cols)
while turns > 0:
if not ship_rows or not ship_cols:
print("Congradulations! You sank my battleship!")
break
print("Lives Left", turns)
guess_row = int(input("Guess Row: "))
guess_col = int(input("Guess Col: "))
if guess_row in ship_rows and guess_col in ship_cols :
print("You hit my battleship!")
board[guess_row][guess_col] = "#"
ship_rows.remove(guess_row)
ship_cols.remove(guess_col)
else:
if guess_row not in range(len(board)) or guess_col not in range(len(board)):
print("Oops, that's not even in the ocean")
elif board[guess_row][guess_col] == "X":
print("You guessed that one already")
elif board[guess_row][guess_col] == "#":
print("You already hit that part")
else:
print("You missed my battleship!")
board[guess_row][guess_col] = "X"
turns -= 1
print_board(board)
else:
print("You lose!")
for row in ship_rows:
for col in ship_cols:
board[row][col] = "O"
print_board(board)
grades = []
print("Start entering grades and type -1 to end the list")
while True:
user_input = int(input())
if(user_input == -1):
break
else:
grades.append(user_input)
def print_grades(grades_input):
for grade in grades_input:
print grade
def grades_sum(scores):
total = 0
for score in scores:
total += score
return total
def grades_average(grades_input):
sum_of_grades = grades_sum(grades_input)
average = sum_of_grades / float(len(grades_input))
return average
def grades_variance(scores):
average = grades_average(scores)
variance = 0
for score in scores:
variance += (average - score) ** 2
return variance/len(scores)
def grades_std_deviation(variance):
return variance ** 0.5
print_grades(grades)
print(grades_sum(grades))
print(grades_average(grades))
variance = grades_variance(grades)
print grades_std_deviation(variance)
print(grades_variance(grades))