• 周二. 4 月 21st, 2026

物嫩软件资讯网

软件资讯来物嫩

python笔记-对战小游戏(format()函数,for,while,if结合+循环是否再来一局)

admin@wunen

6 月 16, 2025

import time

import random

player_victory=0

enemy_victory=0

play_record=0

while True:

for i in range(1,4):

time.sleep(1)

#format()格式化

print(‘————————–第{0}局游戏开始———————————–‘.format(i))

player_life=random.randint(1,150)

player_attack=random.randint(30,50)

enemy_life=random.randint(1,150)

enemy_attack=random.randint(30,50)

print(‘【玩家】血量:’+str(player_life)+’ 攻击:’+str(player_attack))

print(‘————————‘)

time.sleep(1.5)

print(‘【敌人】血量:’+str(enemy_life)+’ 攻击:’+str(enemy_attack))

print(‘————————‘)

while(player_life>=0)and(enemy_life>=0):

player_life=player_life-enemy_attack

enemy_life=enemy_life-player_attack

print(‘你发起了攻击,【敌人】剩余血量’+str(enemy_life))

print(‘敌人向你发起了攻击,【玩家】剩余血量’+str(player_life))

print(‘————————‘)

time.sleep(1.5)

if player_life>0 and enemy_life<=0:

print(‘敌人死翘翘了,你赢了’)

player_victory+=1

elif player_life<=0 and enemy_life>0:

print(“悲催,敌人把你干掉了!”)

enemy_victory+=1

else:

print(‘啊呀,你和敌人平手了’)

time.sleep(1)

print(‘——————————————————————–‘)

if player_victory>enemy_victory:

print(‘恭喜你,你赢了!’)

elif player_victory<enemy_victory:

print(‘呀,好可惜~你被打败了’)

else:

print(‘继续努力,你和敌人打平手了’)

answer=str(input(‘要不要再来一局?请回答:是 否\n’))

if answer==’是’:

continue

else:

break

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注