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"
有帮助吗?

解决方案

This returns a boolean:

return ans[0] in "yY"

Think of it as saying:

if ans[0] in "yY":
    return True
else:
    return False

其他提示

It does return a boolean, try this:

ans = 'Yes'
ans[0] in 'yY'

That resolves to True. It is evaluating whether the first character in string ans is Y or y.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top