Help With Python Please :)

12 replies
Hey Guys,

So I am required to make a really basic python game. The python dice game. Yes I know its probably laughably simple for most of you but I am not a technical person so I really need some help. Basically the game is your supposed to enter in your name and a guess of what you think the 6 diced rolled will total (guess a number and you win if the total of the dice roled is greater than your guess). I got the form all figured out in the xhtml but when I select go it just brings me to my code for the python script i.e its not really connect properly so my questions are
1) do i need to enter in the name of the html document anywhere on the python document
2) I keep getting a syntax error in my python document when I run the module..I have no idea. why... i might be doing a few things wrong. can some one please let me know

Here is my python script

import cgi
form = cgi.FieldStorage()

import random
die = random.randint(a,b)

name = form.getvalue("name")
value=(form.getvalue ("value"))
total = int(form.getvalue("total")
die1 = random.randint (1, 6)
die2 = random.randint (1, 6)
die3 = random.randint (1, 6)
die4 = random.randint (1, 6)
die5 = random.randint (1, 6)
total = int(die1) + int(die2) + int(die3) + int(die4) + int(die5)

# 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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Dice Game</title>
</head>
<body>
"""


#print HTML body using form data
print "<p>Thanks for playing, " + name + ". </p>"
print "<p>You bet the total would be at least " + str(value) +" . </p>"
print "<p>The total rolled was " + str(total) + ". </p>"
if die1 == 1:
print "<p><img src="' + str(die1) + 'die.png" alt="' + str(die1) + '" /></p>"
elif die1 == 2:
print "<p><img src="' + str(die2) + 'die.png" alt="' + str(die2) + '" /></p>"
elif die1 == 3:
print "<p><img src="' + str(die3) + 'die.png" alt="' + str(die3) + '" /></p>"
elif die1 == 4:
print "<p><img src="' + str(die4) + 'die.png" alt="' + str(die4) + '" /></p>"
elif die1 == 5:
print "<p><img src="' + str(die5) + 'die.png" alt="' + str(die5) + '" /></p>"
elif die1 == 6:
print "<p><img src="' + str(die6) + 'die.png" alt="' + str(die6) + '" /></p>"
if total == value:
print "<p> You Won! </p>"
elif value < total:
print "<p> You Won! </p>"
else:
print "<p> Sorry....You Lost.</p>"
print "</body></html>"


HERE IS MY HTML SCRIPT

<!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>
<link rel="stylesheet" href="a4.css" />
</head>
<body>
<h1>Dice Game</h1>
<form action="dice.py">
<div class="formin">Enter your name:
<input type="text" size="6" maxlength="30" name= "name" />
</div>
<div class="formin"> You bet the total will be at least:
<input type= "text" size="6" maxlength="3" name="value" />
</div>
<div class="formin">
<input type= "submit" value="Go!" />
</div>
</form>
</body>
</html>



IF someone could help me I'd really appreciate it, and of course I'd try and return the favor. I'm really good at PPC if that helps

Brittany
#idle #programming #python
  • {{ DiscussionBoard.errors[1459523].message }}
  • Profile picture of the author btl1
    Thanks, I'll try that and hope I get some response

    Britt
    {{ DiscussionBoard.errors[1459556].message }}
  • Profile picture of the author Mr. Enthusiastic
    Hi Brittany,

    You have several things going on here. I suggest you master them one a time. When I was in driver ed they said, "separate your hazards."

    First, write a program that only runs on the console or command line. Get it to say "Hello, world!" Then, get it to ask your name, and greet you by name. Then, get it to make a random die roll and show you the number. Then, get it to ask your guess and then compare that to the number.

    Once all of this works flawlessly, but not until then, make your web page. Get your Python program to say "Hello, Web!" Then, get it to ask your name using a web form, and greet you by name on the web. Continue until your dice game is hooked up to your web site.

    Small bites, crawl-walk-run, etc.

    Chris
    {{ DiscussionBoard.errors[1460022].message }}
  • Profile picture of the author btl1
    Thank Chris. I'll try that. I've made a number of improvements now...I think im close but something still isn't connecting

    import cgi
    form = cgi.FieldStorage()
    import cgi
    form = cgi.FieldStorage()

    import random


    name = form.getvalue("name")
    value=(form.getvalue ("value"))
    die1 = random.randint (1, 6)
    die2 = random.randint (1, 6)
    die3 = random.randint (1, 6)
    die4 = random.randint (1, 6)
    die5 = random.randint (1, 6)
    total = int(die1+die2+die3+die4+die5)

    # 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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Dice Game</title>
    </head>
    <body>
    """


    #print HTML body using form data
    print "<p>Thanks for playing, " + name + ". </p>"
    print "<p>You bet the total would be at least " + str(value) +" . </p>"
    print "<p>The total rolled was " + str(total) + ". </p>"
    if die1 == 1:
    print "<p><img src='" + str(die1) + "die.png' alt='" + str(die1) + "' /></p>"
    elif die1 == 2:
    print "<p><img src='" + str(die2) + "die.png' alt='" + str(die2) + "' /></p>"
    elif die1 == 3:
    print "<p><img src='" + str(die3) + "die.png' alt='" + str(die3) + "' /></p>"
    elif die1 == 4:
    print "<p><img src='" + str(die4) + "die.png' alt='" + str(die4) + "' /></p>"
    elif die1 == 5:
    print "<p><img src='" + str(die5) + "die.png' alt='" + str(die5) + "' /></p>"
    elif die1 == 6:
    print "<p><img src='" + str(die6) + "die.png' alt='" + str(die6) + "' /></p>"
    if total == value:
    print "<p> You Won! </p>"
    if value < total:
    print "<p> You Won! </p>"
    else:
    print "<p> Sorry....You Lost.</p>"
    print "</body></html>"
    {{ DiscussionBoard.errors[1460112].message }}
  • Profile picture of the author highrider21
    I'm no programming guru or anything, but I would think this would be easier in php. Instead of trying to get your python code to work with your html page you could just write the entire page and program in php.
    Signature

    {{ DiscussionBoard.errors[1469172].message }}
    • Profile picture of the author chaos69
      1) do i need to enter in the name of the html document anywhere on the python document
      I'm not sure exactly what problem you are having with this - the syntax looks ok....also it works fine from a copy/paste into a new cgi on my server.

      2) I keep getting a syntax error in my python document when I run the module..I have no idea. why... i might be doing a few things wrong. can some one please let me know
      If it's echoing the py script back to the browser, you either do not have the correct handler setup for it in your webserver or you are trying to execute it outside of the cgi directory. [If using apache, look for the ScriptAlias path in the .conf]
      Signature
      Best Ways To Make Money Online

      Eight bytes walk into a bar. The bartender asks, “Can I get you anything?”
      “Yeah,” reply the bytes. “Make us a double.”
      {{ DiscussionBoard.errors[1469737].message }}
    • Profile picture of the author Mr. Enthusiastic
      Originally Posted by highrider21 View Post

      I'm no programming guru or anything, but I would think this would be easier in php. Instead of trying to get your python code to work with your html page you could just write the entire page and program in php.
      The original post said "I am required to..." so I think it's a homework assignment for a programming class.
      {{ DiscussionBoard.errors[1470353].message }}
    • Profile picture of the author wisywig
      Python is much cleaner, tidier syntax and more concise - if the other poster doesn't know either, python is much simpler to learn.

      The only advantage to php really is that there is a lot of sample code out there for web-based tasks. Not to say python can't do those tasks just as well or better, it just seems that much of the sample code is in php. Perhaps that's because php people tend to need more help, though. :-)

      (I'm currently trying to find my way around Wordpress code.... aaaaargh... can't see the wood for the trees with all the extra characters required for the syntax).
      {{ DiscussionBoard.errors[1472757].message }}
  • Profile picture of the author wisywig
    Try here for some help. Particularly note the point about line endings - a common gotcha!

    4.2 Hello world!

    {{ DiscussionBoard.errors[1472808].message }}
  • Profile picture of the author btl1
    Hey Guys,

    Thanks so much for the help and sorry for the slow responses as I have been traveling back to Canada from Europe. Your right it was a homework assignment of sorts. I signed up for an introduction to the web type of class in order to help me get some basic html and css skills. ITs been REALLY useful (don't plan on becoming pro anytime soon but useful for making small changes to my sites ect). The last section of the class has been on python and yes its a lot easier to learn intially then PHP.

    After a while I figured it all out. Thank you so much for al lyour help!!
    {{ DiscussionBoard.errors[1474140].message }}
    • Profile picture of the author Mr. Enthusiastic
      Originally Posted by btl1 View Post

      After a while I figured it all out.
      Hey, post your working code! We saw the "before" that didn't work, I think we deserve to see that "after" that does work.
      {{ DiscussionBoard.errors[1475237].message }}
  • Profile picture of the author btl1
    Mr. Enthusiastic I love your enthusiasm . I will post what i did later today! I'm actually just on my way out to my programming exam now ---ahhhh wish me luck
    {{ DiscussionBoard.errors[1483238].message }}

Trending Topics