开发者

I can't figure out the problem in this code about python, need help

I'm going to create a game, user put in the name and bet number and my program is gonna show the bet number is greater than the sum of 5 random dice and show each picture of them. I know there is something wrong but I dont know where

import cgi
import random

formData = cgi.FieldStorage()
playerName = formData["name"].value
playerGuess = int(formData["guess"].value)
theLength = 5
index =1
print "Content-type: text/html"


print "<p>Thanks for playing, " + playerName + ",</p>"
for die in range(theLength):
    die%i = random.randint(1,6)%index
    if int(die) == 1:
        print "<img src = "Images/dice-1.gif" alt="1" width="107" height="107" />"
    elif int(die) == 2:
        print "<img src = "Images/dice-2.gif" alt="2" width="107" height="107" />"
    elif int(die) == 3:
        print "<img src = "Images/dice-3.gif" alt="3" width="107" height="107" />"
    elif int(die) == 4:
        print "<img src = "Images/dice-4.gif" alt="4" width="107" height="107" />"
    elif int(die) == 5:
        print "<img s开发者_运维百科rc = "Images/dice-5.gif" alt="5" width="107" height="107" />"
    elif int(die) == 6:
        print "<img src = "Images/dice-6.gif" alt="6" width="107" height="107" />"
    index = index + 1
    total = die%i + die%i

print "<p>You bet the total would be at least " + playerGuess + ". The total rolled was " + sum + ".</p>"

if int(guessNumber) >= total:
    print "<p>You won!</p>"
else:
    print """<p>Sorry, you lose!</p>


The line

die%i = random.randint(1,6)%index

Is not valid python syntax. It's cousin a few lines below:

total = die%i + die%i

Will not give you the results you're expecting; the %'s will be treated as mod operators. That loop should be something more like:

for die in range(theLength):
    val = random.randint(1,6)
    print '<img src = "Images/dice-%i.gif" alt="%i" width="107" height="107" />' % (val, val)

Even with those changes, it will still exception because sum is an undefined variable when you use it to print the total. You'll need to keep track of it in your "roll loop."

sum = 0
for die in range(theLength):
    val = random.randint(1,6)
    print "<img src = 'Images/dice-%i.gif" alt="%i" width="107" height="107" />' % (val, val)
    total = total + val


You're not sending correct HTTP answers; after the final header, a newline is supposed to come, so you're looking for:

print("Content-Type: text/plain")
print("")


Also, the first %i in

die%i = random.randint(1,6)%index

is a syntax error; the left side has to be a reference to a variable.


And instead of total = die%i + die%i, you probably wanted total += die%index and total = 0 at the very top.


By the way, you can greatly simplify the big if .. elif block by writing

print "<img src = "Images/dice-%s.gif" alt="1" width="107" height="107" />" % die


The line is invalid because you want to assign to a result of an operation.

die%i = random.randint(1,6)%index

For example if say die is 5 and i is 1 then it would result that:

0 = random.randint(1,6)%index

Which doesn't make any sense.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜