繁体   English   中英

如何使我的代码在我的一个 function 中包含两个“while”循环?

[英]How do I make it so my code includes both "while" loops in my one function?

我正在尝试将我的两个 WHILE 循环放入一个 function。 如果我将它缩进一个,它不会检查第二个,因为第一个中断,如果损坏大于或等于生成的“斜线”的值,它将停止重复代码。 我怎样才能把它合二为一?

(忽略任何伤害值或除斜线以外的移动)

move_1 =  input("You and the demon are head on. You pull out your weapon, what is your first move: Slug, Slash, Swing, Pierce\n")


slash = "slash"
swing = "swing"
whirl= "whirl"
slug = "slug"

damage_slash = (random.randint(20,25))
damage_swing = (random.randint(15,30))
damage_whirl = (random.randint(10,40))
damage_slug = (random.randint(5,50))


## slash move code

while True:
        if move_1 == slash:
            print("You slash your {} your opponent for {}".format(user_weapon_1, damage_slash))
        break

while True:
        if damage_slash >= 23:
            print("you hit a darn critical")
        break

根据您的代码,您似乎正在尝试编写一个回合制格斗游戏。 我认为你想要的更像是这样的:

while True:
  user_weapon_1 = input("weapon what for test?")
  move_1 =  input("You and the demon are head on. You pull out your weapon, what is your first move: Slug, Slash, Swing, Pierce\n")

  slash = "slash"
  swing = "swing"
  whirl= "whirl"
  slug = "slug"

  damage_slash = (random.randint(20,25))
  damage_swing = (random.randint(15,30))
  damage_whirl = (random.randint(10,40))
  damage_slug = (random.randint(5,50))


  ## slash move code

  if move_1 == slash:
    print("You slash your {} your opponent for {}".format(user_weapon_1, damage_slash))

  if damage_slash >= 23:
    print("you hit a darn critical")

  # Put your end condition here to break the loop.
  # e.g. `if total_damage > 100`
  if ???:
    break # This will end the outer while loop

请注意,只有一个while循环,它不断询问玩家选择的武器和选择的攻击。 如果他们 select 是斜线武器,它会打印出攻击摘要。 如果损坏足够高,它会打印出额外的消息。 然后整个事情无限期地重复。

但是,不清楚您希望循环何时结束,即什么条件结束攻击? 例如,您可能想跟踪变量中的累积伤害,并在伤害超过阈值时结束攻击。 我留下了??? 在代码中表示此缺失条件。

暂无
暂无

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

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