Python - 二十一点
def showCards():
#SUM
sum = playerCards[0] + playerCards[1]
#Print cards
print "Player's Hand: " + str(playerCards) + " : " + "sum"
print "Dealer's Hand: " + str(compCards[0]) + " : " + "sum"
compCards = [Deal(),Deal()]
playerCards = [Deal(),Deal()]
如何将包含值的列表的整数元素相加? #SUM 错误下可以组合整数等列表...
def showCards():
#SUM
sum = playerCards[0] + playerCards[1]
#Print cards
print "Player's Hand: " + str(playerCards) + " : " + "sum"
print "Dealer's Hand: " + str(compCards[0]) + " : " + "sum"
compCards = [Deal(),Deal()]
playerCards = [Deal(),Deal()]
How can i add up the integer element of a list containing to values? under #SUM error is can combine lists like ints...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了上面提到的注释之外, sum 实际上是 Python 中的一个内置函数,它所做的正是您似乎正在寻找的功能 - 所以不要覆盖它并将其用作标识符名称!而是使用它。
还有一个所有 Python 程序员都应该遵循的风格指南 - 它有助于进一步区分 Python 代码和用其他语言(例如 Perl 或 PHP)编写的代码中经常遇到的难以理解的代码。 Python 中有一个更高的标准,但你没有达到它。 样式
因此,这里重写了您的代码以及一些需要填写的猜测缺失的部分。
好吧,所以我得意忘形,实际上写了整件事。正如另一位发帖者所写, sum(list_of_values) 是可行的方法,但实际上对于黑杰克规则来说太简单了。
Aside from the comments mentioned above, sum is actually a built in function in Python that does just what you seem to be looking for - so don't overwrite it and use it as an identifier name! Instead use it.
Also there's a style guide that all Python programmers are meant to follow - it's what helps to further distinguish Python code from the inscrutable sludge often encountered in code written in other languages such as say Perl or PHP. There's a higher standard in Python and you're not meeting it. Style
So here's a rewrite of your code along with some guesses to fill in the missing parts.
Well ok so I got carried away and actually wrote the whole thing. As another poster wrote sum(list_of_values) is the way to go but actually is too simplistic for the Black Jack rules.
要在这里找到一手牌的价值,您可以执行类似的操作
,但看起来您可能已经在帖子的第二部分提到#SUM 中尝试过这样做,我不知道您想说什么。仅当 Deal() 返回整数时这才有效。
To find the value of a hand here you can just do something like
But it looks like you might have tried that from the second part of your post mentioning #SUM, I don't know what you were trying to say. This will only work if Deal() returns integers.