Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 12th, 2006, 1:11 PM   #11
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Your code looks pretty good

Here's a different approach, using classes, functions and sets. It's a little longer, I think, and it's perhaps not as obvious what's doing what. But it might be of use in demostrating a different path to the same solution.
import random

def random_line(filename):
	"Returns a random line from a file."
	file = open(filename)
	try:		return random.choice(file.readlines())
	finally:	file.close()

class AlreadyGuessed(Exception):
	def __init__(self, letters):
		self.letters = letters

class Word(object):
	def __init__(self, word, lives = 10):
		self.word    = word
		self.guesses = set()
		self.lives   = lives

	def guess(self, letters):
		"Guess letter(s) in the word."
		same = set(letters).intersection(self.guesses)
		if same: raise AlreadyGuessed(same)
		self.guesses.add(letters)
		self.lives -= len([l for l in letters if l not in self.word])

	@property
	def lost(self): return self.lives == 0

	@property
	def won(self):	return set(self.word).issubset(self.guesses)

	@property
	def wrong_guesses(self):
		return word.guesses.difference(set(self.word))
		
	def __str__(self):
		"Returns a string showing how much the user has guessed of the word."
		return " ".join(l not in self.guesses and "_" or l for l in self.word)

if __name__ == '__main__':
	word = Word(random_line("words.txt").strip().lower())

	while not word.lost and not word.won:
		print "Word:", word
		print "Wrong guesses:", " ".join(word.wrong_guesses)
		print "Lives left:", word.lives
		print
		try:
			word.guess(raw_input("Please enter a letter: ").lower())
		except AlreadyGuessed, e:
			print "Already guessed:", " ".join(e.letters)

	if word.lost:	print "You've lost."
	elif word.won:	print "You've won."

	print "The word was:", word.word
Arevos is offline   Reply With Quote
Old May 17th, 2006, 1:47 PM   #12
demon101
Hobbyist Programmer
 
demon101's Avatar
 
Join Date: Mar 2006
Location: westboro, ohio
Posts: 159
Rep Power: 0 demon101 is an unknown quantity at this point
Send a message via Yahoo to demon101
import cherrypy

class HelloWorld:
    def index(self):
        return "Hello world!"
    index.exposed = True

cherrypy.root = HelloWorld()
cherrypy.server.start()

what does this do?
__________________
Demon101 Production's

Code Forums
demon101 is offline   Reply With Quote
Old May 17th, 2006, 2:26 PM   #13
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
It prints "Hello world" if you have CherryPy as a web development framework. It's only good for the OP if the player's puzzle answer is "Hello world".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:17 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC