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"
try:
guess = int(data[data.find('=')+1:])
except:
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">
</form>'''
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?