Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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 如何创建一个始终运行但不';不要干扰代码的运行顺序?_Python_Loops - Fatal编程技术网

Python 如何创建一个始终运行但不';不要干扰代码的运行顺序?

Python 如何创建一个始终运行但不';不要干扰代码的运行顺序?,python,loops,Python,Loops,我正在用Python制作一个文本冒险游戏来开始,因为我还不太擅长Python。我已尝试实施一套健康规则: while True: if health == maxHealth: playerAtMaxHealth = True; elif health > maxHealth: health = maxHealth; elif health < 0: health = 0 elif health == 0:

我正在用Python制作一个文本冒险游戏来开始,因为我还不太擅长Python。我已尝试实施一套健康规则:

while True:
   if health == maxHealth:
       playerAtMaxHealth = True;
   elif health > maxHealth:
       health = maxHealth;
   elif health < 0:
       health = 0
   elif health == 0:
       playerDead = True;
为True时:
如果health==maxHealth:
playerAtMaxHealth=True;
elif health>maxHealth:
健康=最大健康;
elif健康状况<0:
健康=0
elif运行状况==0:
playerDead=True;
下面是我的代码的其余部分:

def startUp():
  StartPRINT("""╔═══╗
║╔══╝
║╚══╦══╦══╦══╦══╦══╗
║╔══╣══╣╔═╣╔╗║╔╗║║═╣
║╚══╬══║╚═╣╔╗║╚╝║║═╣
╚═══╩══╩══╩╝╚╣╔═╩══╝
─────────────║║
─────────────╚╝ \n""")
  global gold
  global health
  global inventory
  global berryCollected;
  global maxHealth;
  global playerAtMaxHealth;
  global playerDead
  berryCollected = False;
  playerAtMaxHealth = True;
  playerDead = False
  gold = 0
  health = 30
  maxHealth = 30
  inventory = []
  time.sleep(2)
  while True:
    if health == maxHealth:
      playerAtMaxHealth = True;
    elif health > maxHealth:
      health = maxHealth;
    elif health < 0:
      health = 0
    elif health == 0:
      playerDead = True;
  forest()
def启动():
StartPRINT(“”)╔═══╗
║╔══╝
║╚══╦══╦══╦══╦══╦══╗
║╔══╣══╣╔═╣╔╗║╔╗║║═╣
║╚══╬══║╚═╣╔╗║╚╝║║═╣
╚═══╩══╩══╩╝╚╣╔═╩══╝
─────────────║║
─────────────╚╝ \(以“)号填列)
全球黄金
全球卫生
全球库存
全球柏木采集;
全球卫生;
全球卫生;
全球玩家头
berryCollected=False;
playerAtMaxHealth=True;
playerDead=False
黄金=0
健康=30
maxHealth=30
存货=[]
时间。睡眠(2)
尽管如此:
如果health==maxHealth:
playerAtMaxHealth=True;
elif health>maxHealth:
健康=最大健康;
elif健康状况<0:
健康=0
elif运行状况==0:
playerDead=True;
森林()

基本上,它会打印出开始文本,但它只是卡在while循环中,不会移动到forest()。

我不知道您是否考虑过这一点,但以面向对象的方式进行操作可能会更好。因此,将英雄或玩家作为职业,其属性为黄金、健康等。然后,您可以很容易地拥有一个功能,您可以在战斗或互动后运行该功能,以检查玩家是否还活着

不要使用while true来覆盖健康规则。 所以如果你想检查玩家的健康状况是这样的。。 虽然是真的 森林()//也许有人会和怪物搏斗 //这里有健康检查

它会在森林里做一些健康检查。。。。
如果你写的像你的一样,它只会在运行状况检查中卡住

你可以简单地将while循环包装在一个线程中。然后启动线程,它不会改变代码的其他结构。 用Python创建线程非常容易。您需要导入
线程
然后可以使用
threading.thread()
函数创建线程 您可以创建任意数量的线程,只需使用
start()
函数启动即可。它们都将在不干扰代码的情况下执行


有关python线程的更多详细信息,请参阅。

您认为什么会结束循环?为什么会有循环?您是否正在寻找多线程?