Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

python - 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 attributes.
  • The difference is divided by five and rounded down to create a ‘strength modifier’.
  • The same process is carried out for the skill attribute and named ‘skill modifier’.
  • Then the game begins:
    • Both players throw a six sided die.
    • If the values are the same, no changes happen.
    • If the values are different, the player with the highest value then gains the strength and skill worked out earlier and it is then added to their total. The player who has the lowest value has the strength and skill worked out taken away from their total.

This must repeat until the strength attribute is equal or below 0, however, this part I cant do, I know how to loop but this is always done by asking the user if they wish to go again.

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

Please help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let me introduce you to the while loop. You use this when you want something to occur if the specified condition is true or false.

I briefly edited your code.

import random

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()

I gave players 1 and 2 initial values bet these can be changed to your liking. To round down, I made another variable and changed strength to an integer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...