0% found this document useful (0 votes)
20 views6 pages

Book Programs and Challenges of Chapter 6

Uploaded by

Vasu Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Book Programs and Challenges of Chapter 6

Uploaded by

Vasu Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#Printing game instructions using function

def instructions():
"""display game instructions"""
print("""
here is the game
""")

print("here are the instructions")


instructions()
print("here they are again")
instructions()

#Receive and return program

# Function to display a message


def display(message):
print(message)

# Function to return the value 5


def give_me_five():
five = 5
return five

# Function to ask a yes or no question


def ask_yes_no(question):
"""Ask a yes or no question."""
response = None
while response not in ("y", "n"):
response = input(question).lower()
return response

# Main part of the program


# Call the display function
display("Here's a message for you.\n")

# Call the give_me_five function and store the result in the variable 'number'
number = give_me_five()
# Print the result
print("Here's what I got from give_me_five():", number)

# Call the ask_yes_no function with a question and store the result in the variable
'answer'
answer = ask_yes_no("\nPlease enter 'y' or 'n': ")
# Print the result
print("Thanks for entering:", answer)
#global scope program
def read_global():
print("from inside global scope, value is:",value)

def shadow_global():
value=-10
print("now the value is:",value)

def change_global():
global value
value=-10
print("the value has been changed to:",value)

value=10
read_global()
print("the value is:",value)

shadow_global()
print("but the value in global scope is:",value)

change_global()
print("the global scope value is now also",value)

#tictac toe game


x = "X" # Changed to uppercase
o = "O" # Changed to uppercase
empty = ' '
tie = 'Tie' # Changed to uppercase
num_squares = 9

def display_instruct():
"""Display game instructions."""
print(""" Welcome to the greatest intellectual challenge of all time:
Tic-Tac-Toe.
This will be a showdown between your human brain and my silicon processor.
You will make your move known by entering a number, 0 - 8. It will correspond
to the board position as illustrated:

The number 0 | 1 | 2
--------
3 | 4 | 5
--------
6 | 7 | 8

Prepare yourself, human.


The ultimate battle is about to begin. \n""")
def ask_yes_no(question):
"""Ask a yes or no question"""
response = None # Fixed the typo (none to None)
while response not in ["y", "n"]: # Fixed the condition
response = input(question)
return response

def ask_number(question, high, low):


"""Ask a number in a range"""
response = None
while response not in range(low, high):
response = int(input(question))
return response

def pieces():
"""Ask if the human wants to go first"""
go_first = ask_yes_no("Do you want to play first human? ")
if go_first == "yes":
print("Okay, you go first.")
human = x
computer = o
else:
computer = x
human = o
return computer, human

def new_board():
"""Create a new board"""
board = []
for square in range(num_squares):
board.append(empty)
return board

def display_board(board):
"""Display board on screen"""
print(board[0], "|", board[1], '|', board[2])
print("---------")
print(board[3], '|', board[4], '|', board[5]) # Fixed the typo (changed 6 to
5)
print('---------')
print(board[6], '|', board[7], '|', board[8])

def legal_moves(board):
moves = []
for square in range(num_squares):
if board[square] == empty:
moves.append(square)
return moves

def winner(board):
"""Determine the game winner."""
ways_to_win = ((0, 1, 2),
(3, 4, 5),
(6, 7, 8),
(0, 3, 6),
(1, 4, 7),
(2, 5, 8),
(0, 4, 8),
(2, 4, 6))

for row in ways_to_win:


if board[row[0]] == board[row[1]] == board[row[2]] != empty:
winner = board[row[0]]
return winner
if empty not in board:
return tie

return None

def human_move(board, human):


legal = legal_moves(board)
move = None
while move not in legal:
move = ask_number("Where do you want to put your move: ", num_squares, 0)
# Fixed the order of arguments
if move not in legal:
print("That place is already used. Choose another.")
print("Fine.")
return move

def computer_move(board, computer, human):


# Make a copy to work with since the function will be changing the list
board = board[:] # Fixed the syntax error

BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)
print("I shall take square number", end=" ")

for move in legal_moves(board):


board[move] = computer
if winner(board) == computer:
print(move)
return move
board[move] = empty

for move in legal_moves(board):


board[move] = human
if winner(board) == human:
print(move)
return move
board[move] = empty

for move in BEST_MOVES: # Fixed the variable name (best_moves to BEST_MOVES)


if move in legal_moves(board):
print(move)
return move

def next_turn(turn):
"""Switch turns"""
if turn == x:
return o
else:
return x

def congrat_winner(winner, human, computer):


"""Congratulate the winner"""
if winner != tie:
print(winner, "you won")
else:
print("It's a tie")

if winner == computer:
print("The computer won, unfortunately.")

elif winner == human:


print("The human won!")

elif winner == tie:


print("It's a tie")

def main():
display_instruct()
computer, human = pieces()
turn = x
board = new_board()
display_board(board)
while not winner(board):
if turn == human:
move = human_move(board, human)
board[move] = human
else:
move = computer_move(board, computer, human)
board[move] = computer
display_board(board)
turn = next_turn(turn)
the_winner = winner(board)
congrat_winner(the_winner, computer, human)

main()

#guess my number using ask_number and main() functions


import random

def ask_number(low,high):
"""guess number in this range"""

number = int(input("Player's number: "))


guess = 0
while guess < 3:
compnumber = random.randint(low,high)
print("Computer's guess:", compnumber)

if compnumber > number:


print("Guess lower")
elif compnumber < number:
print("Guess higher")
else:
print("You won!")
break # Exit the loop if the computer guessed correctly

guess += 1

if compnumber != number:
print(f"Computer couldn't guess the number. It was {number}.")

def main():
"""main function play the guess my number game"""
print("welcome to guess my number game")
low_range = int(input("Enter the low range: "))
high_range = int(input("Enter the high range: "))
ask_number(low_range, high_range)

main()

You might also like