这个错误在 Python 和 XHTML 中意味着什么?
它一直显示此错误,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看来您没有
POST
它是一个猜测
值。您之前的页面中应该有类似这样的内容:It looks like you're not
POST
ing it aguess
value. You should have something like this in the page before:您的代码期望
guess
的值位于表单数据字典中,但该值不存在。Your code expects a value for
guess
to be in the form data dictionary, but it's not present.这意味着该表单没有“猜测”条目,但有“名称”条目。
It means that the form doesn't have a "guess" entry, but it does have a "name" entry.
用户输入的字典
formData
中没有“guess”
条目。您确定您提交的表单中有一个类似的字段吗?
There is no
"guess"
entry in theformData
, 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?在 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 codeformData["guess"]
tries to access the dictionaryformData
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 yourcgi.FieldStorage()
call, this can be interpreted as saying that your cgi form object has no field with the name "guess".