質問

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