这个错误在 Python 和 XHTML 中意味着什么?

发布于 2024-11-28 14:51:13 字数 1086 浏览 0 评论 0原文

它一直显示此错误,

Traceback (most recent call last):
  File "/home/zca22/public_html/Lab_Assn_5/Scripts/dice.py", line 7, in <module>
    playerGuess = int(formData["guess"].value)
  File "/usr/lib/python2.6/cgi.py", line 541, in __getitem__
    raise KeyError, key
KeyError: 'guess'

我不知道我的代码出了什么问题。

import cgi
import random

formData = cgi.FieldStorage()
playerName = formData["name"].value
playerGuess = int(formData["guess"].value)
theLength = 5
sum = 0

print "Content-type: text/html"

print "<p>Thanks for playing, " + playerName + ",</p>"

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 + val

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

if playerGuess >= sum:
    print "<p>You won!</p>"
else:
    print """<p>Sorry, you lose!</p>
      </body>
      </html>"""

It keep showing this error

Traceback (most recent call last):
  File "/home/zca22/public_html/Lab_Assn_5/Scripts/dice.py", line 7, in <module>
    playerGuess = int(formData["guess"].value)
  File "/usr/lib/python2.6/cgi.py", line 541, in __getitem__
    raise KeyError, key
KeyError: 'guess'

I don't know what's wrong iin my code.

import cgi
import random

formData = cgi.FieldStorage()
playerName = formData["name"].value
playerGuess = int(formData["guess"].value)
theLength = 5
sum = 0

print "Content-type: text/html"

print "<p>Thanks for playing, " + playerName + ",</p>"

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 + val

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

if playerGuess >= sum:
    print "<p>You won!</p>"
else:
    print """<p>Sorry, you lose!</p>
      </body>
      </html>"""

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

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

发布评论

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

评论(5

埋葬我深情 2024-12-05 14:51:13

看来您没有POST它是一个猜测值。您之前的页面中应该有类似这样的内容:

<form action="/cgi-bin/guess.py" method="post">
    <dl>
        <dt><label for="name_field">Name:</label></dt>
        <dd><input type="text" id="name_field" name="name" required="required" /></dd>
        <dt><label for="guess_field">Guess:</label></dt>
        <dd><input type="number" id="guess_field" name="guess" min="1" max="6" step="1" required="required" /></dd>
    </dl>
    <p><input type="submit" value="Guess" /></p>
</form>

It looks like you're not POSTing it a guess value. You should have something like this in the page before:

<form action="/cgi-bin/guess.py" method="post">
    <dl>
        <dt><label for="name_field">Name:</label></dt>
        <dd><input type="text" id="name_field" name="name" required="required" /></dd>
        <dt><label for="guess_field">Guess:</label></dt>
        <dd><input type="number" id="guess_field" name="guess" min="1" max="6" step="1" required="required" /></dd>
    </dl>
    <p><input type="submit" value="Guess" /></p>
</form>
半衬遮猫 2024-12-05 14:51:13

您的代码期望 guess 的值位于表单数据字典中,但该值不存在。

Your code expects a value for guess to be in the form data dictionary, but it's not present.

爱*していゐ 2024-12-05 14:51:13

这意味着该表单没有“猜测”条目,但有“名称”条目。

It means that the form doesn't have a "guess" entry, but it does have a "name" entry.

羁拥 2024-12-05 14:51:13

用户输入的字典 formData 中没有 “guess” 条目。您确定您提交的表单中有一个类似 的字段吗?

There is no "guess" entry in the formData, the dictionary of user inputs. Are you sure you have a field like <input name="guess" value="3" /> in the exact form you're submitting?

花期渐远 2024-12-05 14:51:13

在 Python dict 字典数据类型中,每个条目都有一个和一个。您的代码 formData["guess"] 尝试访问键 "guess" 下的字典 formData 以检索基础值。

由于您收到 KeyError,因此您的字典中没有名为 "guess" 的键。由于字典是由您的 cgi.FieldStorage() 调用填充的,因此这可以解释为您的 cgi 表单对象没有名为“guess”的字段。

In the Python dict dictionary data type, each entry has a key and a value. Your code formData["guess"] tries to access the dictionary formData under the key "guess" to retrieve the underlying value.

Since you're getting a KeyError, your dictionary has no key called "guess". Since the dictionary is populated by your cgi.FieldStorage() call, this can be interpreted as saying that your cgi form object has no field with the name "guess".

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