Question

> import random
> import cgi
> form=cgi.FieldStorage()
>
> print """Content-type: text/html
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
>        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html>
> <head>
>    <title>Dice Game</title>
> </head>
> <body>
> <h1>Dice Game</h1>
> """
>
> name = form.getvalue("name")
> bet = int(form.getvalue("bet"))
> double = bet*2
>
> die1=random.randint(1,6)
> die2=random.randint(1,6)
>
> print "<p>Thanks for playing, " + name + ". Your roll:</p>"
> print "<img src=dice-" + str(die1) + ".gif alt=" + str(die1) + ' width="107" height="107" />'
> print "<p>Your opponent's roll:</p>"
> print print "<img src=dice-" + str(die2) + ".gif alt=" + str(die2) + ' width="107" height="107" />'
>
> if die1 > die2:
>    print "<p>You win " + str(bet) + ".</p>"
> if die1 < die2:
>    print "<p>You lose " + str(bet) + ".</p>"
> if die1 == die2:
>    print "<p>You win " + str(double) + ".</p>"
>
>print "</body></html>"
>

Hi, I am just starting to learn Python programming and XHTML. I am doing a interactive web page called Dice Game. I want to have a python web script that gets value of NAME and BET AMOUNT from form, and then generates a random number from 1 to 6 first. Then, the browser displays a dice image that corresponds to that random number, and tells the player if he loses or wins or doubles the bet. In order to display the image accordingly, I used:

> print "<img src=dice-" + str(die1) + ".gif alt=" + str(die1) + ' width="107" height="107" />'

I can run the game without any problems on the website. However, I then used W3C validator and it tells me that I have 7 errors: http://i57.tinypic.com/6qb3er.png

My TA told me that there is a problem of mismatch of quotes, but I don't know the rules of quotes in quotes. I did some researches and got even more confused.... Can someone help me please? :(

Was it helpful?

Solution

There are several issues in your HTML (minor issues) first:

Declare the html tag as:

<html xmlns="http://www.w3.org/1999/xhtml">

Then, I'd change all your string concatenation by expressions like 'xxx att="%s" xxx' % mysubstr:

print "<p>Thanks for playing, %s. Your roll:<br/>" % name
print '<img src="dice-%s.gif" alt="%s" width="107" height="107" /></p>' % (str(die1), str(die1))
print "<p>Your opponent's roll:<br/>"
print '<img src="dice-%s.gif" alt="%s" width="107" height="107" /></p>' % (str(die2), str(die2))

if die1 >= die2:
    print "<p>You win %s.</p>" % str(bet)
if die1 < die2:
    print "<p>You lose %s.</p>" % str(bet)
if die1 == die2:
    print "<p>You win %s.</p>" % str(double)

In this way is easier to detect missing "'s. You need them to encapsulate all your html attribute values.

I've test these changes and the output is something like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Dice Game</title>
</head>
<body>
<h1>Dice Game</h1>

<p>Thanks for playing, Rob. Your roll:<br/>
<img src="dice-3.gif" alt="3" width="107" height="107" /></p>
<p>Your opponent's roll:<br/>
<img src="dice-5.gif" alt="5" width="107" height="107" /></p>
<p>You lose 234.</p>
</body>
</html>

That code passed successfully the W3C validator.

I hope this can help.

OTHER TIPS

Should be more like this:

print '<img src="dice-{}.gif" alt="{}" width="107" height="107" />'.format(die1, die1)

This way is easier to read, and gives you double-quotes on all your attribute values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top