簡體   English   中英

沒有While循環的Python 2.7.3代碼循環

[英]Python 2.7.3 Code Loops Without a While Loop

我對編程還比較陌生,所以如果代碼中出現一些小小的打ic,請不要感到驚訝,但是正在發生的問題是,這段代碼似乎是循環的,整個過程都是循環的,我不明白為什么。 我看了運行此功能的實際代碼,但看起來還不錯。 因此,我無法在此代碼中找到任何導致其自身循環的錯誤。 (如果問題不在此代碼中,則在其下面循環代碼)

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)

循環代碼:

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?: "))

謝謝!

thebeast的末尾看你的return聲明。 在函數末尾,您再次調用該函數! 由於每次調用時都會發生這種情況,因此您將永遠不會停止調用它(直到達到最大遞歸深度)。 考慮到您沒有在randomevents捕獲到thebeast的返回值,您應該考慮它不返回任何東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM