关于python中骰子扑克的简单问题
我正在按照教科书示例构建骰子扑克游戏。下面是一段我不太明白的代码片段,但它确实有效。那么在run方法下的while循环中,第二个条件是TextInterface类中的wantToPlay方法必须为true,对吗?但是当我查看wantToPlay方法时,没有给出布尔结果,即没有给出它是否为真。有人可以解释这是如何工作的吗?
class PokerApp:
def run(self):
while self.money >= 10 and self.interface.wantToPlay():
self.playRound()
class TextInterface:
def wantToPlay(self):
ans = input("do you wish to try your luck? ")
return ans[0] in "yY"
I'm following along a textbook example of building a dice poker game. Below is a snippet of code I don't quite understand but it does work. So in the while loop under the run method, the second condition is that the wantToPlay method in the TextInterface class must be true, correct? But when I look at the wantToPlay method, there is no boolean result i.e. whether it's true or not is not given. Can someone explain how this works?
class PokerApp:
def run(self):
while self.money >= 10 and self.interface.wantToPlay():
self.playRound()
class TextInterface:
def wantToPlay(self):
ans = input("do you wish to try your luck? ")
return ans[0] in "yY"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将返回一个布尔值:
可以将其视为:
This returns a boolean:
Think of it as saying:
它确实返回一个布尔值,试试这个:
解析为
True
。它正在评估字符串 ans 中的第一个字符是Y
还是y
。It does return a boolean, try this:
That resolves to
True
. It is evaluating whether the first character in string ans isY
ory
.