[Tutor] Python Programming for the absolute beginner 3e Ch3 Challenge 4

2013-08-08 Thread John Feleppa
Hello,

I am working through Michael Dawson's book, "Python Programming for the
absolute beginner 3rd edition".

Have just completed Chapter 3, but cannot solve Challenge 4.

Has anyone solved this yet - and be willing to share this code?

I would much appreciate..

John.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Programming for the absolute beginner 3e, Ch3 Challenge 4

2013-08-11 Thread John Feleppa
Thanks for getting back to me...

I am using Python 3.3.2.

The challenge is as follows:

# Chapter 3 Challenge 4
#
# Write the psudocode for a program where the player and the computer
# trade places in the number guessing game.  That is, the player picks a
# random number between 1 and 100 that the computer has to guess.
# Before you start, think about how you guess.  If all goes well, try
# coding the game.

It builds on the previous challenge, where the user guesses the number - I
coded that OK, as below.

import random

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")

# set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1

# guessing loop

while guess != the_number and tries < 10:
if guess > the_number:
print("Lower...")
else:
print("Higher...")

guess = int(input("Take a guess: "))
tries += 1

if guess == the_number:
print("You guessed it!  The number was", the_number)
print("And it only took you", tries, "tries!\n")
else:
print("Huh - you took ", tries, "tries and still never got it.")
print("It was ", the_number, " - but why tell you, you'll forget it,
won't you.")

input("\n\nPress the enter key to exit.")
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Michael Dawson Chapter 6 Challenge 4

2014-11-23 Thread John Feleppa
Dear all,

Has anyone solved the fourth challenge in Chapter 6 of Michael Dawson's
book, 'Python Programming for the absolute beginner'?

The challenge: 'Write a new *computer_move()* function for the Tic-Tac-Toe
game to plug the hole in the computer's strategy.  See if you can create an
opponent that is unbeatable!'

The function is as follows:

def computer_move(board, computer, human):
"""Make computer move."""
# make a copy to work with since function will be changing list
board = board[:]
*# the best positions to have, in order*
*BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)*

print("I shall take square number", end=" ")

# if computer can win, take that move
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print(move)
return move
# done checking this move, undo it
board[move] = EMPTY

# if human can win, block that move
for move in legal_moves(board):
board[move] = human
if winner(board) == human:
print(move)
return move
# done checkin this move, undo it
board[move] = EMPTY

*# since no one can win on next move, pick best open square*
*for move in BEST_MOVES:*
*if move in legal_moves(board):*
*print(move)*
*return move*

I believe a solution lies in the final lines, which I put in bold, and in
the BEST_MOVES tuple, which is also in bold.  As in, it looks through the
list of best moves in order regardless of what move the opponent has made.
So it just dumbly selects the next item in the tuple instead of responding
to the opponent's move.  So, for example, the following sequence shows the
computer's weakness:

a. the opponent goes first in a corner, in Square 0
b. the computer picks the centre, in Square 5
c. the opponent picks the opposite corner, Square 8
d. the weakness - the computer picks the corner, Square 2, which will be
easily countered by the opponent going in Square 6, instead of the smarter
Square 1, which will result in a draw, and not a loss.

HAS ANYONE SOLVED THIS?  I'd much appreciate help here.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor