如果计分放在循坏的外面,错误就会更多:
import time,random
player_victory = 0 #空白计分板跳出循环外
enemy_victory = 0
while True: #不确定次数的循环
player_life = random.randint(100,150)
enemy_life = random.randint(100,150)
print('玩家血量:%s;敌人血量:%s。'%(player_life,enemy_life))
print('-------------------------------')
time.sleep(1.5)
for i in range(3):
player_attack = random.randint(30,50) #玩家攻击力每次都是随机的
enemy_attack = random.randint(30,50)
print('这是第%s局游戏'%(i+1))
player_life = player_life - enemy_attack
enemy_life = enemy_life - player_attack
print('玩家血量:%s,受到攻击-%s;敌人血量:%s,受到攻击-%s。'%(player_life,enemy_attack,enemy_life,player_attack)) #for-in 循环结束
time.sleep(1.5)
if player_life > 0 and enemy_life < player_life: #玩家大于敌人血量视为赢
player_victory += 1
print('玩家赢了(血量:%s)'%player_life)
elif enemy_life > 0 and player_life < enemy_life: #敌人大于玩家血量视为赢
enemy_victory += 1
print('敌人赢了(血量:%s)'%enemy_life)
else:
print('同归于尽')
print('-------------------------------')
time.sleep(1.5)
if player_victory > enemy_victory:
print('最终结果:玩家赢了')
elif player_victory < enemy_victory:
print('最终结果:敌人赢了')
else:
print('最终结果:平局')
print('-------------------------------')
time.sleep(1.5)
again = input('是否再来一局?请回答是or否')
if again == '是':
continue
else:
break
|