I am trying to make a number guessing game in Python. The correct number is 42. Right now I have it so that it gives you a message when you are too high, too low or right on. I want to add two other messages. One message for no input in the text field that I've created and one message if the input from the user is not a valid number (a string or something). Here is the code I have so far

import sys
print 'Content-Type: text/html' 
print ''

print '<pre>'
# Read the form input which is a single line
guess = -1 
data = sys.stdin.read()
# print data 
if data == []:
    print "Welcome to Josh's number game"
    guess = int(data[data.find('=')+1:]) 
    guess = -1 
print 'Your guess is', guess 
answer = 42 
if guess < answer :
    print 'Your guess is too low' 
if guess == answer:
    print 'Congratulations!' 
if guess > answer :
    print 'Your guess is too high' 
print '</pre>'
print '''<form method="post" action="/"> 
Enter Guess: <input type="text" name="guess"><br> 
<input type="submit">

This line is the message that I want to print if there is no input in the field, but I can't seem to figure out how to get Python to realize that I want it to print that when there is no input. Does it have something to do with the fact that the guess is set as -1 from the beginning? Our professor said to use that so I am guessing that that is not the problem. Is there some syntax that I can use that I am not aware of?

Recommended Answers

How about using cgi module? When I entered it goes ofter POST of the input to my main index page.

Jump to Post

All 4 Replies

How about using cgi module? When I entered it goes ofter POST of the input to my main index page.

Hi, I'm not sure what a CGI module is, can you explain?

cgi module

Something like this:

import cgi
import cgitb; cgitb.enable()  # for troubleshooting
import traceback
import os
import sys
import random

    correct = int(open('secret.txt').read())
except IOError:
    correct = random.randint(1,100)
    with open('secret.txt', 'w') as f:
    print "Content-type: text/html"
    print """

<head><title>Number guessing</title></head>

  <form method="post" action="guess.cgi">
    <p>Number: <input type="text" name="guess"/> 
               VALUE = "Try!"> </form> """

    form = cgi.FieldStorage()
    guess = int(form.getvalue("guess", "-1"))

    if guess > -1:
        print '<p><body bgcolor="#ffffff">'
        print ('<h2>Low</h2>' if guess < correct else 
                     ('<h2>High</h2>' if guess > correct else '<h1><strong>Concratulations!</strong></h1>'))

    print "</body></html>"
    if correct == guess:

    tb = traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)
    tb = ''.join(tb)
    print '<pre>%s</pre></body></html>' % tb

OK, here's the thing: Python, by itself, is not a language designed specifically for web-page design, the way the (for example) PHP or Javascript are. It can be used for this purpose, but you need to import some particular tools for it, namely the cgi module.

What CGI stands for is Common Gateway Interface, and it is a way to let the HTTP server run programs on the server automatically when the program file is requested, rather than serving it to the browser. Python is just one of the languages that can be used with CGI. The HTTP server, for it's own part, usually needs to have the CGI script in a specific directory and has other settings for how it has to be handled so that it knows that the Python source file is a CGI script and not some general program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.