Skip to content Skip to sidebar Skip to footer

How Do I Loop A Code Until A Certain Number Is Created?

This task is to determine the difference between two attributes, strength and skill, from game characters. The process for this is: Determining the difference between the strength

Solution 1:

Just so you get the picture: (in pseudocode)

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"

Not sure if this is what you are looking for exactly. If the c2sk and c1sk is supposed to be randomly reassigned each time it should be inside the loop.

Solution 2:

Add another loop to do the reductions...

c1sc=random.randint(1,6)
c2sc=random.randint(1,6)
if c1sc > c2sc:
    while c2st > 0and 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 > 0and 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=0elif c2sk < 0:
    c2sk=0if c1st < 0:
    print('Character 1 died. Game over.')
elif c2st < 0:
    print('Character 2 died. Game over.')

Of course, there are much more compact ways of writing this. This still isn't ideal but is a reduction in code...

c1sc=random.randint(1,6)
c2sc=random.randint(1,6)
if c1sc != c2sc:
    while c1st > 0andand 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.')

And finally, assuming a re-roll every round...

while c1st > 0andand 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.')

Post a Comment for "How Do I Loop A Code Until A Certain Number Is Created?"