【问题标题】:How do i loop a code until a certain number is created?如何循环代码直到创建某个数字?
【发布时间】:2013-11-15 09:57:57
【问题描述】:

此任务是确定游戏角色的力量和技能两个属性之间的差异。这个过程是:

  • 确定强度属性之间的差异。
  • 差值除以 5 并向下舍入以创建“强度修正值”。
  • 对技能属性执行相同的过程并命名为“技能修饰符”。
  • 然后游戏开始:
    • 两名玩家都投掷六面骰子。
    • 如果值相同,则不会发生任何变化。
    • 如果数值不同,则数值最高的玩家将获得先前计算的力量和技能,然后将其添加到他们的总和中。数值最低的玩家的力量和技能会从他们的总和中扣除。

这必须重复,直到强度属性等于或低于 0,但是,这部分我不能做,我知道如何循环,但这总是通过询问用户是否希望再次进行。

c1sc=random.randint(1,6)
c2sc=random.randint(1,6)
if c1sc > c2sc:
    c1st=c1st+strengthmodifier
    c2st=c2st-strengthmodifier
    c1sk=c1sk+skillmodifier
    c2sk=c2sk-skillmodifier
    print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
    print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
elif c2sc > c1sc:
    c1st=c1st-strengthmodifier
    c2st=c2st+strengthmodifier
    c1sk=c1sk-skillmodifier
    c2sk=c2sk+skillmodifier
    print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
    print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
else:
    print('It\'s a draw, no changes were made.')
if c1sk < 0:
    c1sk=0
elif c2sk < 0:
    c2sk=0

if c1st < 0:
    print('Character 1 died. Game over.')
elif c2st < 0:
    print('Character 2 died. Game over.')

请帮忙!

【问题讨论】:

    标签: python loops random python-3.x


    【解决方案1】:

    让我向您介绍while 循环。如果指定条件为真或假,则当您希望某事发生时,您可以使用它。

    我简要编辑了您的代码。

    随机导入

    def main():
     player1=20
     player2=12
     strength=(20-10)/5
     strength_mod=int(strength)
     while player1>0 and player2>0:
      dice1=random.randint(1,6)
      dice2=random.randint(1,6)
      if dice1>dice2:
        player1+=strength_mod
        player2-=strength_mod
        print("Player 1 strength is:  ", player1)
        print("Player 2 strength is:  ", player2)
      elif dice1<dice2:
        player2+=strength_mod
        player1-=strength_mod
        print("Player 1 strength is:  ", player1)
        print("Player 2 strength is:  ", player2)
    
    main()
    

    我给玩家 1 和 2 的初始值打赌,这些可以更改为您的喜好。为了向下取整,我制作了另一个变量并将强度更改为整数。

    【讨论】:

      【解决方案2】:

      只是为了让你得到图片:(在伪代码中)

      while (c2sk > 0 && c1sk > 0)
          //do complete everything up until the if statements
      if c1st <0
          print "c1st died" 
      elif c2st <0
          print "c2st died"
      

      不确定这是否正是您要寻找的。如果 c2sk 和 c1sk 应该在每次它应该在循环内时被随机重新分配。

      【讨论】:

      • while 可能永远不会是真的 - 如果它们每轮减少 2 并从奇数开始呢?
      • @Basic 我明白你的意思。我不知道是整场比赛应该重播还是只是转牌? c1sc 应该每轮随机重新分配?因为那么它应该在循环内
      • 你没看错 - 我只是假设他们永远以同样的速度死去......
      【解决方案3】:

      添加另一个循环来进行归约...

      c1sc=random.randint(1,6)
      c2sc=random.randint(1,6)
      if c1sc > c2sc:
          while c2st > 0 and c2sk > 0:
              c1st=c1st+strengthmodifier
              c2st=c2st-strengthmodifier
              c1sk=c1sk+skillmodifier
              c2sk=c2sk-skillmodifier
              print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
              print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
      elif c2sc > c1sc:
          while c1st > 0 and c1sk > 0:
              c1st=c1st-strengthmodifier
              c2st=c2st+strengthmodifier
              c1sk=c1sk-skillmodifier
              c2sk=c2sk+skillmodifier
              print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
              print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
      else:
          print('It\'s a draw, no changes were made.')
      if c1sk < 0:
          c1sk=0
      elif c2sk < 0:
          c2sk=0
      
      if c1st < 0:
          print('Character 1 died. Game over.')
      elif c2st < 0:
          print('Character 2 died. Game over.')
      

      当然,还有更简洁的写法。这仍然不理想,但减少了代码......

      c1sc=random.randint(1,6)
      c2sc=random.randint(1,6)
      if c1sc != c2sc:
          while c1st > 0 and and c2st > 0:
              c1st += strengthmodifier if c1sc > c2sc else -1 * strengthmodifier
              c2st += strengthmodifier if c1sc < c2sc else -1 * strengthmodifier
              c1sk += skillmodifier if c1sc > c2sc else -1 * skillmodifier
              c2sk += skillmodifier if c1sc < c2sc else -1 * skillmodifier
              print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
              print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
          c1sk = math.max(c1sk, 0) # Clamp skill to a minimum of 0
          c2sk = math.max(c2sk, 0) # ...
      
          if c1st < c2st:
              print('Character 1 died. Game over.')
          else:
              print('Character 2 died. Game over.')
      else:
          print('It\'s a draw, no changes were made.')
      

      最后,假设每轮都重​​新掷骰...

      while c1st > 0 and and c2st > 0:
          c1sc=random.randint(1,6)
          c2sc=random.randint(1,6)
          if c1sc != c2sc:
              c1st += strengthmodifier if c1sc > c2sc else -1 * strengthmodifier
              c2st += strengthmodifier if c1sc < c2sc else -1 * strengthmodifier
              c1sk += skillmodifier if c1sc > c2sc else -1 * skillmodifier
              c2sk += skillmodifier if c1sc < c2sc else -1 * skillmodifier
              print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
              print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
          else:
              print('It\'s a draw, no changes were made.')
      
      
          c1sk = math.max(c1sk, 0) # Clamp skill to a minimum of 0
          c2sk = math.max(c2sk, 0) # ...
      
      if c1st < c2st:
          print('Character 1 died. Game over.')
      else:
          print('Character 2 died. Game over.')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-10
        • 2013-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多