我无法弄清楚这段关于 python 的代码中的问题,需要帮助

发布于 2024-11-28 14:46:28 字数 1405 浏览 1 评论 0原文

我要创建一个游戏,用户输入名称和赌注号码,我的程序将显示赌注号码大于 5 个随机骰子的总和,并显示它们的每张图片。我知道有问题,但不知道问题出在哪里

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 src = "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>

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 src = "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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小鸟爱天空丶 2024-12-05 14:46:28

该行

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

不是有效的 python 语法。下面几行是表弟:

total = die%i + die%i

不会给你你期望的结果; % 将被视为 mod 运算符。该循环应该更像是:

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)

即使进行了这些更改,它仍然会异常,因为当您使用 sum 打印总计时,它是一个未定义的变量。您需要在“滚动循环”中跟踪它。

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

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
我做我的改变 2024-12-05 14:46:28

您没有发送正确的 HTTP 答案;在最后一个标题之后,应该出现一个换行符,因此您正在寻找:

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

另外,第一个 %i

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

是语法错误;左侧必须是对变量的引用。

您可能想要的是 total += die%indextotal = 0,而不是 total = die%i + die%i顶部。

顺便说一句,您可以通过编写来大大简化大 if .. elif

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

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
剧终人散尽 2024-12-05 14:46:28

该行无效,因为您想要分配给操作的结果。

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

例如,如果 die 为 5,i 为 1,那么结果将是:

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

这没有任何意义。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文