I'm an extreme beginner with Python (this game is the first time I have ever seen it) so please bear with me.

Creating a simple dice game. Place a bet. Three dice are rolled, if any are the same value you lose. If you win, you get double your bet.

I've gotten the majority of the game programmed but I cannot figure out how to input the bet data from my xhtml form into the final results.

import cgi
form = cgi.FieldStorage()

nombre = form.getvalue("nombre")
bet = form.getvalue("bet")

# print HTTP/HTML headers
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>
"""

# print HTML body using form data
import random
die = random.randint(1,6)
die2 = random.randint(1,6)
die3 = random.randint(1,6)

bet = int( form.getvalue("bet") )

print "<p>Thanks for playing, "+ nombre +". Your roll:</p>"
print "<p> %i %i %i.</p>" % (die, die2, die3)
if die == die2:
    print "<p>You lose $%i.</p>" %i (bet)
if die2 == die3:
    print "<p>You lose $%i.</p>" %i (bet)
if die == die3:
    print "<p>You lose $%i.</p>" %i (bet)
else:
    print "<p>You win $&i.</p>" %i(bet*2)

print """</body></html>"""

When the game is played, it comes up with the error "NameError: name 'i' is not defined".

I'm sure there's a very simple error I'm making but it's brought my progress to an absolute standstill.

I also have to substitute images for the text numbers and am unsure of how to proceed with that.

Any help would be MUCH appreciated. And for reference, this is how the game should function.

Recommended Answers

All 11 Replies

Alright, I figured out my mistake in not converting the int into string. So there's one problem down. Here's my code:

import cgi
form = cgi.FieldStorage()

nombre = form.getvalue("nombre")
bet = int (form.getvalue("bet") )

# print HTTP/HTML headers
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>
"""

# print HTML body using form data
import random
die = random.randint(1,6)
die2 = random.randint(1,6)
die3 = random.randint(1,6)

print "<p>Thanks for playing, "+ nombre +". Your roll:</p>"
print "<p> %i %i %i.</p>" % (die, die2, die3)
if die == die2 or die2 == die3 or die == die3:
    print "<p>You lose $" + str(bet) +".</p>"
if die != die3 and die != die2 and die2 != die3:
    print "<p>You win $" + str(bet*2) +".</p>"

print """</body></html>"""

The only thing I'm clueless on is how to replace the displayed numbers with images.

I would say you have to get/make 6 images, something like this:
dice-1.gif
dice-2.gif
dice-3.gif
dice-4.gif
dice-5.gif
dice-6.gif

I would just download the images from the website you gave us.

That would be the place to start.

Yes, I have the images, I'm just unsure of how to input the code to display them, because it's conditional on which random number is displayed. I need to replace the text it's currently outputting with images.

# print HTML body using form data
import random
die1 = random.randint(1,6)
die2 = random.randint(1,6)
die3 = random.randint(1,6)
dielist = [die1,die2,die3]

bet = int( form.getvalue("bet") )

print "<p>Thanks for playing, "+ nombre +". Your roll:</p>"
print "<p> %i %i %i.</p>" % dielist

dups = bool([die for die in dielist
             if dielist.count(die) > 1])
#True if there are duplicates in the list

if dup:
    print "<p>You lose $%i.</p>" % (bet)
else:
    print "<p>You win $%i.</p>" % (bet)


print """</body></html>"""

I think that's a better way to write the code.

# print HTML body using form data
import random
die1 = random.randint(1,6)
die2 = random.randint(1,6)
die3 = random.randint(1,6)
dielist = [die1,die2,die3]

bet = int( form.getvalue("bet") )

print "<p>Thanks for playing, "+ nombre +". Your roll:</p><br/>"
##print "<p> %i %i %i.</p>" % dielist

for die in dielist:
    gifname = "dice-%i.gif" % die
    print '<img src="/path/to/diceimages/%s" />' % gifname

dups = bool([die for die in dielist
             if dielist.count(die) > 1])
#True if there are duplicates in the list

if dup:
    print "<p>You lose $%i.</p>" % (bet)
else:
    print "<p>You win $%i.</p>" % (bet)


print """</body></html>"""

Something like that.
I might have made a mistake in that code, since I typed it up quickly.

That code assumes that in the path:
/path/to/diceimages/
you have the files:
dice-1.gif
dice-2.gif
dice-3.gif
dice-4.gif
dice-5.gif
dice-6.gif

fantastic! That code worked so much better. Thank you!

Can I please have this thread deleted??

>Can I please have this thread deleted?

No. You/We can't.

I sent a pm with what this is regarding. Is there anyone I can contact who can remove or edit this?

I sent a pm with what this is regarding. Is there anyone I can contact who can remove or edit this?

Could you not just mark the thread SOLVED as the starter of the thread?

I sent a pm with what this is regarding. Is there anyone I can contact who can remove or edit this?

We can't delete this thread as per the site rules/acceptable use policy. You asked for help, you got help. Deleting this thread would deprive others of the hard work done and the time spent by jcao219 in answering this thread.

Quoting the 'Acceptable use policy' of this site:

Additionally, DaniWeb LLC reserves full rights and privileges to information posted to anywhere within the daniweb.com domain by its members and staff. Any and all information posted on DaniWeb may not be copied or used elsewhere, in any way, shape, or form, either on the Internet or in print, without prior written permission from Dani Horowitz.

In case your instructor demands an explanation, you are more than welcome to re-direct him to this post.

Be a part of the DaniWeb community

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