Book Programs and Challenges of Chapter 6
Book Programs and Challenges of Chapter 6
def instructions():
"""display game instructions"""
print("""
here is the game
""")
# 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)
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
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))
return None
BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)
print("I shall take square number", end=" ")
def next_turn(turn):
"""Switch turns"""
if turn == x:
return o
else:
return x
if winner == computer:
print("The computer won, unfortunately.")
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()
def ask_number(low,high):
"""guess number in this range"""
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()