简体   繁体   English

没有While循环的Python 2.7.3代码循环

[英]Python 2.7.3 Code Loops Without a While Loop

I am relatively new to programming, so don't be surprised if there are some minor hiccups in the code, but the problem that is happening is that this code seems to loop itself, the whole thing and I don't understand why. 我对编程还比较陌生,所以如果代码中出现一些小小的打ic,请不要感到惊讶,但是正在发生的问题是,这段代码似乎是循环的,整个过程都是循环的,我不明白为什么。 I looked at the actual code that runs this function but it seemed fine. 我看了运行此功能的实际代码,但看起来还不错。 So I cannot find any error in this code that makes it loop itself. 因此,我无法在此代码中找到任何导致其自身循环的错误。 (If the problem isn't in this code then the looping code it beneath it) (如果问题不在此代码中,则在其下面循环代码)

def thebeast(yourhp):
  foe = "Thisisirrelevant"
  enemy = int(random.randint(1,4))
  if enemy == 1:
    foe = "skeleton"
  elif enemy == 2:
    foe = "man with crazy eyes"
  else:
    foe = "dog, a big and scary dog"
  monsteract = 1
  dmg = 0
  print "-=~-=~-=~-=~-=~-=~-=~-=~-=~-=~-=~-=~"
  time.sleep(0.1)
  print "           C O M B A T"
  time.sleep(0.1)
  print "-=~-=~-=~-=~-=~-=~-=~-=~-=~-=~-=~-=~"
  time.sleep(0.1)
  print "You encounter a " + foe
  time.sleep(0.5)
  while monsteract == 1:
    comb = str(input("What do you do to the " + foe + "?" + " Do you feel     like jabbing it or stabbing it?"))
    if comb in ["s", "S", "Stab", "STAB", "stab"]:
      if spear == 1:
        dmg = int(random.randint(1,4))
      elif sword == 1:
        dmg = int(random.randint(1,3))
      else:
        dmg = int(random.randint(1,5))
    elif comb in ["j", "J", "Jab", "JAB", "jab"]:
      if spear == 1:
        dmg = int(random.randint(1,3))
      elif sword == 1:
        dmg = int(random.randint(1,4))
      else:
        dmg = int(random.randint(1,5))
      if dmg == 1:
        print "You slay the " + foe + " with graceful ease"
        time.sleep(0.5)
        monsteract = 0
      else:
        enemydmg = int(random.randint(1,3))
        print "The " + foe + " strikes you for " + str(enemydmg) + " damage"
        time.sleep(0.3)
        print "That didn't work out as planned, but you pull yourself together and prepare to strike the " + foe
        time.sleep(0.3)
        yourhp = yourhp - enemydmg
        if yourhp < 0:
          yourhp = 0
        print "You have " + str(yourhp) + " health left"
        time.sleep(0.3)
        if yourhp < 1:
          print "The " + foe + " has slain you, well done, you're dead, on the bright side the innocent "
          print foe + " is still alive! Every life counts, even that of a " + foe + "."
          monsteract = 0
  return thebeast(yourhp)

Looping code: 循环代码:

def randomevents(yourhp):
    turn = 10
    while turn > 0:
        time.sleep(0.1)
        happening = int(random.randint(1,6))
        time.sleep(0.1)
        if yourhp < 1:
            turn = 0
            print "You managed to escape the dungeon, by dying that is"
        elif happening == 1:
            turn = turn - 1
            thebeast(yourhp)
        elif happening == 2:
            item(sword, spear)
            turn = turn - 1
        elif happening in [3, 4, 5]:
            friend()
            turn = turn - 1
    print "Well done! You escaped the dungeon! (Either by running out of it, or by having your soul sent to another world)"
    useless = str(input("Are you satsified with yourself?: "))

Thank you! 谢谢!

Look at your return statement at the end of thebeast . thebeast的末尾看你的return声明。 At the end of the function, you call the function again! 在函数末尾,您再次调用该函数! Since this happens every time you call it, you will never stop calling it (until you hit the maximum recursion depth). 由于每次调用时都会发生这种情况,因此您将永远不会停止调用它(直到达到最大递归深度)。 Considering that you don't capture the return value of thebeast in randomevents , you should consider it not returning anything. 考虑到您没有在randomevents捕获到thebeast的返回值,您应该考虑它不返回任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM