0% found this document useful (0 votes)
7 views

python L6 worksheet 1

The document contains Python code for a simple household chore program and a Tic Tac Toe game. It includes functions for washing dishes, vacuuming, making the bed, and managing game actions like drawing the grid, player choices, and checking for wins. The Tic Tac Toe game features a board representation and logic for player and computer moves, but the computer's move logic is not implemented.

Uploaded by

mzama21
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

python L6 worksheet 1

The document contains Python code for a simple household chore program and a Tic Tac Toe game. It includes functions for washing dishes, vacuuming, making the bed, and managing game actions like drawing the grid, player choices, and checking for wins. The Tic Tac Toe game features a board representation and logic for player and computer moves, but the computer's move logic is not implemented.

Uploaded by

mzama21
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

def wash_up():

print("Washing the dishes...")

def vacuum():
print("Vacuuming the floor...")

def make_bed():
print("Making the bed...")

wash_up()
vacuum()
make_bed()

# Tic Tac Toe Game

def drawGrid():
print("---------")
print("| |")
print("| 1 |")
print("| |")
print("---------")
print("| |")
print("| 2 |")
print("| |")
print("---------")
print("| |")
print("| 3 |")
print("| |")
print("---------")

def playerChooses():
print("Choose a square (1-9): ")
playerChoice = input()
print("Player chose:", playerChoice)

def checkIfSomeoneWon():
print("Checking if someone won...")

def computerChooses():
print("Computer choosing a square...")

# Start the game


drawGrid()
playerChooses()
checkIfSomeoneWon()
computerChooses()
checkIfSomeoneWon()
# Tic Tac Toe Game

# The board is represented as a list of lists


board = [[" " for _ in range(3)] for _ in range(3)]

# Function to print the board


def drawGrid():
print("---------")
for row in board:
print("|", end="")
for cell in row:
print(f" {cell} ", end="")
print("|")
print("---------")

# Function to check if a player has won


def checkIfSomeoneWon():
# Check rows
for row in board:
if row[0] == row[1] == row[2] and row[0] != " ":
print(f"{row[0]} wins!")
return True

# Check columns
for col in range(3):
if board[0][col] == board[1][col] == board[2][col] and board[0][col] != " ":
print(f"{board[0][col]} wins!")
return True

# Check diagonals
if (board[0][0] == board[1][1] == board[2][2] and board[0][0] != " ") or \
(board[0][2] == board[1][1] == board[2][0] and board[0][2] != " "):
print(f"{board[1][1]} wins!")
return True

# Check for a draw


if all(cell != " " for row in board for cell in row):
print("It's a draw!")
return True

return False

# Function to get the player's move


def playerChooses():
while True:
try:
choice = int(input("Choose a square (1-9): "))
if 1 <= choice <= 9:
break
else:
print("Invalid choice. Please choose a number between 1 and 9.")
except ValueError:
print("Invalid input. Please enter a number.")

# Convert choice to row and column indices


row = (choice - 1) // 3
col = (choice - 1) % 3

# Check if the square is empty


if board[row][col] == " ":
board[row][col] = "X"
print("Player chose:", choice)
else:
print("That square is already taken. Please choose again.")

# Function to get the computer's move


def computerChooses():
# ... (Implement the computer's move logic here)
print("Computer choosing a square...")

# Start the game


drawGrid()
playerChooses()
checkIfSomeoneWon()
computerChooses()
checkIfSomeoneWon()

You might also like