Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在while循环中将程序的控制传递回调用函数_Python_While Loop_Python 3.4 - Fatal编程技术网

Python 如何在while循环中将程序的控制传递回调用函数

Python 如何在while循环中将程序的控制传递回调用函数,python,while-loop,python-3.4,Python,While Loop,Python 3.4,我有一个小的视觉扑克骰子应用程序,我正在工作,我添加了一个帮助屏幕,澄清规则和支出。我在一个while循环中运行我的程序,所以现在只要按下help按钮,它就不会退出循环,即使帮助功能已经完成。不知道确切的原因。以下是两个功能: class PokerApp(object): def __init__(self, interface): self.dice = Dice() self.money = 100 self.interface =

我有一个小的视觉扑克骰子应用程序,我正在工作,我添加了一个帮助屏幕,澄清规则和支出。我在一个while循环中运行我的程序,所以现在只要按下help按钮,它就不会退出循环,即使帮助功能已经完成。不知道确切的原因。以下是两个功能:

class PokerApp(object):

    def __init__(self, interface):
        self.dice = Dice()
        self.money = 100
        self.interface = interface

    def run(self):
        choice = self.interface.want_to_play()
        while self.money >= 10 and choice != "Quit":
            if choice == "Help":
                self.interface.help()
                self.run()
            else:
                self.play_round()
        self.interface.close()
然后我的帮助功能:

def help(self):
        rule_format = "{value:<20}{reward:>5}".format
        help_screen = Rectangle(Point(100, 300), Point(500, 100))
        help_screen.setFill("gray")
        help_screen.draw(self.win)
        help_title = Text(Point(300, 125), "Dice Poker Rules")
        help_title.setSize(16)
        help_title.setStyle("bold")
        help_title.draw(self.win)
        value_reward = [("Two Pairs", 5), ("Three of a Kind", 8), ("Full House", 12),
                        ("Four of a Kind", 15), ("Straight", 20), ("Five of a Kind", 30)]
        rule_start = [200, 260]
        rules = []
        for value, reward in value_reward:
            rules.append(Text(Point(*rule_start), rule_format(value=value, reward=reward)))
            rule_start[1] -= 20

        for index, item in enumerate(rules):
            item.draw(self.win)

        while True:
            p = self.win.getMouse()
            while not p:
                continue
            help_screen.undraw()
            help_title.undraw()
            [rule.undraw() for rule in rules]
def帮助(self):
规则_format=“{value:5}”。格式
帮助屏幕=矩形(点(100300),点(500100))
帮助屏幕。设置填充(“灰色”)
帮助屏幕绘制(self.win)
help_title=文本(点数(300125),“骰子扑克规则”)
帮助标题。设置大小(16)
help_title.setStyle(“粗体”)
帮助标题。抽签(self.win)
价值奖励=[(“两对”,5),(“三对一类”,8),(“满屋”,12),
(“四个一类”,15),(“直的”,20),(“五个一类”,30)]
规则_start=[200260]
规则=[]
对于价值,在价值中奖励\u奖励:
规则.append(文本(点(*规则\u开始),规则\u格式(值=值,奖励=奖励)))
规则_开始[1]-=20
对于索引,枚举中的项(规则):
项目.抽签(自赢)
尽管如此:
p=self.win.getMouse()
虽然不是p:
持续
help_screen.undraw()
帮助_title.undraw()
[rule.undraw()用于规则中的规则]

首先,您有两个未终止的
while
循环。第一个
while
循环没有结束条件,也没有退出循环的
break
语句。这意味着它永远不会结束。另外,只要
self.win.getMouse()
返回一个错误值,
not p
将变为true,内部
将在
循环运行时执行,因为
p
从不更改,所以永远不执行任何操作。如果打算在
p
变为false时结束
help
功能,则需要将内部
while
循环替换为and
If
语句,并将
continue
更改为中断。另外,您真的想在第一次迭代后取消绘制所有内容吗?更正代码:

while True:
    p = self.win.getMouse()
    if not p:
       break
help_screen.undraw()
help_title.undraw()
[rule.undraw() for rule in rules]

如果有意义的话,我想在他们再次点击屏幕时立即取消绘制所有内容。感谢您对while循环的帮助@flybonzai我不知道self.win.getMouse()
有什么作用,但我推测当它返回一个错误的值时,这意味着帮助屏幕已经结束,此时您想要取消绘制内容。它只返回用户单击位置的x和y坐标。所以基本上第二个while循环是等到他们再次点击。@flybonzai你应该在你的问题中说明这一点。似乎您需要类似于
while self.win.getMouse():pass
,然后是
while not self.win.getMouse():pass
,它将等待鼠标释放后再按下。