You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mod_python-commits@quetz.apache.org by Apache Wiki <wi...@apache.org> on 2006/11/28 05:14:22 UTC

[Mod_python Wiki] Update of "GuessingGame" by ErikThompson

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Mod_python Wiki" for change notification.

The following page has been changed by ErikThompson:
http://wiki.apache.org/mod_python/GuessingGame

New page:
{{{#!python
<%
import random
s = Session.Session(req) # store the session object into the 's' variable
buttonText = "Submit"
if not s.has_key('answer') or not form.has_key('input'):
	s['answer'] = random.randint(1, 100) # create 'answer' session variable
	s['guesses'] = 0 # create 'guesses' session variable 
	message = "Guess a Number Between 1 and 100"
else:
	s['guesses'] += 1 
	guess = form['input'] # retrieve value of 'input' form variable
	if int(guess) == int(s['answer']):
		message = "Correct.  It Took You %s Guesses." % s['guesses']
		if int(s['guesses']) == 1:
			message = message.replace("Guesses","Guess") # we don't want '1 Guesses'	
		del s['guesses'] # remove the guesses session variable
		del s['answer']  # remove the answer session variable
		buttonText = "NEW GAME"
	else:
		if int(guess) < int(s['answer']):
			message = "Your Guess Was Too Low. %s Guesses.  Try Again." % (s['guesses'])		
		else:
			message = "Your Guess Was Too High. %s Guesses. Try Again." % (s['guesses'])
		if int(s['guesses']) == 1:
			message = message.replace("Guesses","Guess") # we don't want '1 Guesses'
s.save() # save any changes we made to the session variables
%>
<html><head><title>PSP Guessing Game</title</head>
<body onload="document.getElementById('txtInput').focus();">
<h3><%= message %></h3>
<form name="form1" method="post" action="session.psp">
<% 
if buttonText == "Submit":
%>
  <input type="text" id="txtInput" name="input">
<%
# end if (PSP needs this comment to close the html block of the if statement)
%>
  <input type="submit" name="Submit" value="<%= buttonText %>">
</form>
</body></html>
}}}

== Notes ==

This program uses two session variables 'answer' and 'guesses':[[BR]]
answer: stores a random number between 1 and 100[[BR]]
guesses: stores the number of times the user has guessed for the answer.

It uses one form variable 'input'.  That is from the textbox on the html form.

Basically the program starts out by generating a random number and asking the user to guess it.
Each time the user submits a guess the program checks if they have guessed correctly in which case
it displays an appropriate message and allows the user to press the button to start a new game.  If
the user guesses incorrectly it displays "too high, or two low", shows the number of guesses so far,
and asks the user to guess again.

In the original if statement it probably doesn't need the "or not form.has_key('input')".  That was included just to show how to check for the existence of a form variable.