我无法弄清楚这段关于 python 的代码中的问题,需要帮助
我要创建一个游戏,用户输入名称和赌注号码,我的程序将显示赌注号码大于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该行
不是有效的 python 语法。下面几行是表弟:
不会给你你期望的结果;
%
将被视为 mod 运算符。该循环应该更像是:即使进行了这些更改,它仍然会异常,因为当您使用
sum
打印总计时,它是一个未定义的变量。您需要在“滚动循环”中跟踪它。The line
Is not valid python syntax. It's cousin a few lines below:
Will not give you the results you're expecting; the
%
's will be treated as mod operators. That loop should be something more like: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."您没有发送正确的 HTTP 答案;在最后一个标题之后,应该出现一个换行符,因此您正在寻找:
另外,第一个
%i
是语法错误;左侧必须是对变量的引用。
您可能想要的是
total += die%index
和total = 0
,而不是total = die%i + die%i
顶部。顺便说一句,您可以通过编写来大大简化大
if .. elif
块You're not sending correct HTTP answers; after the final header, a newline is supposed to come, so you're looking for:
Also, the first
%i
inis a syntax error; the left side has to be a reference to a variable.
And instead of
total = die%i + die%i
, you probably wantedtotal += die%index
andtotal = 0
at the very top.By the way, you can greatly simplify the big
if .. elif
block by writing该行无效,因为您想要分配给操作的结果。
例如,如果 die 为 5,i 为 1,那么结果将是:
这没有任何意义。
The line is invalid because you want to assign to a result of an operation.
For example if say die is 5 and i is 1 then it would result that:
Which doesn't make any sense.