加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

并行循环Python中的循环

发布时间:2020-12-20 12:16:16 所属栏目:Python 来源:网络整理
导读:我是 Python的新手,一般的编程,我正在为我的妹妹创建一个虚拟宠物风格的游戏. 在python中是否可以运行2 while循环? 例如: while 1: input_event_1 = gui.buttonbox( msg = 'Hello,what would you like to do with your Potato Head?',title = 'Main Screen
我是 Python的新手,一般的编程,我正在为我的妹妹创建一个虚拟宠物风格的游戏.

在python中是否可以运行2 while循环?
例如:

while 1:
    input_event_1 = gui.buttonbox(
        msg = 'Hello,what would you like to do with your Potato Head?',title = 'Main Screen',choices = ('Check Stats','Feed','Exercise','Teach','Play','Go to Doctor','Sleep','Change Favourite Thing','Get New Toy','Quit'))
    if input_event_1 == 'Check Stats':
        myPotatoHead.check_p_h_stats()
    elif input_event_1 == 'Feed':
        myPotatoHead.feed_potato_head()
    elif input_event_1 == 'Exercise':
        myPotatoHead.exercise_potato_head()
    elif input_event_1 == 'Teach':
        myPotatoHead.teach_potato_head(myPotatoHead)
    elif input_event_1 == 'Play':
        myPotatoHead.play_with_toy()
    elif input_event_1 == 'Sleep':
        myPotatoHead.put_p_h_asleep()
    elif input_event_1 == 'Go to Doctor':
        myPotatoHead.doctor_check_up()
    elif input_event_1 == 'Change Favourite Thing':
        myPotatoHead.change_favourite_thing()
    elif input_event_1 == 'Quit':
        input_quit = gui.ynbox(
            msg = 'Are you sure you want to quit?',title = 'Confirm quit',choices = ('Quit','Cancel'))
        if input_quit == 1:
            sys.exit(0)

while 1:
    time.sleep(20)
    myPotatoHead.hunger = str(float(myPotatoHead.hunger) + 1.0)
    myPotatoHead.happiness = str(float(myPotatoHead.happiness) - 1.0)
    myPotatoHead.tiredness = str(float(myPotatoHead.tiredness) + 1.0)

如果没有,是否有某种方法可以将其转换为一个循环?
我希望第二个循环中的东西每20秒发生一次,但第一个循环中的东西会不断发生.

谢谢你的帮助

解决方法

看看 Threading.Timer.

有一个code recipe here to schedule a function to run every 5 seconds.

import thread
import threading

class Operation(threading._Timer):
    def __init__(self,*args,**kwargs):
        threading._Timer.__init__(self,**kwargs)
        self.setDaemon(True)

    def run(self):
        while True:
            self.finished.clear()
            self.finished.wait(self.interval)
            if not self.finished.isSet():
                self.function(*self.args,**self.kwargs)
            else:
                return
            self.finished.set()

class Manager(object):

    ops = []

    def add_operation(self,operation,interval,args=[],kwargs={}):
        op = Operation(interval,args,kwargs)
        self.ops.append(op)
        thread.start_new_thread(op.run,())

    def stop(self):
        for op in self.ops:
            op.cancel()
        self._event.set()

if __name__ == '__main__':
    # Print "Hello World!" every 5 seconds

    import time

    def hello():
        print "Hello World!"

    timer = Manager()
    timer.add_operation(hello,5)

    while True:
        time.sleep(.1)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读