如何循环代码,直到创建了某个数字?

2024-06-16 11:42:46 发布

您现在位置:Python中文网/ 问答频道 /正文

此任务是从游戏角色中确定力量和技能这两个属性之间的区别。其过程是:

  • 确定强度属性之间的差异。在
  • 将差值除以5并四舍五入以创建“强度修饰符”。在
  • 对skill属性执行相同的过程,并命名为“skill modifier”。在
  • 游戏开始了:
    • 两位选手都投了一个六边形骰子。在
    • 如果值相同,则不会发生任何更改。在
    • 如果值不同,那么价值最高的玩家将获得先前计算出的力量和技能,然后将其加到他们的总数中。价值最低的玩家的力量和技能从他们的总价值中扣除。在

这必须重复,直到强度属性等于或低于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.')

请帮忙!在


Tags: your属性isattributestrengthskillprintcharacter
3条回答

只是为了让你了解情况:(用伪代码)

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循环。当您希望在指定的条件为true或false时发生某些事情时,可以使用此选项。在

我简短地编辑了你的代码。在

随机导入

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的初始值打赌这些可以根据你的喜好改变。为了四舍五入,我创建了另一个变量,并将强度改为整数。在

添加另一个循环来进行缩减。。。在

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.')

当然,还有更简洁的写作方式。这仍然是不理想的,但减少了代码。。。在

^{pr2}$

最后,假设每一轮都要重放。。。在

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.')

相关问题 更多 >