|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 1835575828 于 2021-7-5 23:36 编辑
才学三天python 不知道哪里出现了问题
想让两个角色互相对打 作者想让叶孤城有30%的概率释放天外飞仙并眩晕对方一回合,但是按这种代码运行,叶孤城百分百释放了天外飞仙,请大佬解释
关键是while后面第二个if中的continue不知道用的对不对 应该是那里出了问题
import time
import random
class Role:
def __init__(self,name,hp):
self.name=name
self.hp=hp
def tong(self,enemy):
enemy.hp-=10
print('【%s】捅了【%s】一刀'%(self.name,enemy.name))
def kanren(self,enemy):
enemy.hp-=15
print('【%s】砍了【%s】一刀'%(self.name,enemy.name))
def dazhao(self,enemy):
enemy.hp-=30
print('【%s】对【%s】使用天外飞仙,眩晕【%s】一回合'%(self.name,enemy.name,enemy.name))
def chiyao(self):
self.hp+=10
print('【%s】吃了一口药'%(self.name))
def __str__(self):
return '%s还剩下%s的血量'%(self.name,self.hp)
xmcx=Role('西门吹雪',300)
ygc=Role('叶孤城',300)
time.sleep(3)
print('决战紫禁之巅')
print('对战双方:')
print(xmcx.name,'生命:%s'%(xmcx.hp))
print(ygc.name,'生命:%s'%(ygc.hp))
print('战斗开始!')
time.sleep(3)
while True:
if xmcx.hp<=0 or ygc.hp<=0:
break
a=random.randint(1,10)
if a == 1 or 2 or 3:
ygc.dazhao(xmcx)
print(ygc)
print(xmcx)
print('**************************************')
continue
else:
ygc.kanren(xmcx)
print(ygc)
print(xmcx)
print('**************************************')
xmcx.tong(ygc)
print(ygc)
print(xmcx)
print('**************************************')
xmcx.chiyao()
print(ygc)
print(xmcx)
print('**************************************')
time.sleep(1)
print('游戏结束....')
print('西门吹雪获胜')
这句用错了
应该拆开写
if a == 1 or a == 2 or a == 3:
或者
if a in [1,2,3]:
import time
import random
class Role:
def __init__(self,name,hp):
self.name=name
self.hp=hp
def tong(self,enemy):
enemy.hp-=10
print('【%s】捅了【%s】一刀'%(self.name,enemy.name))
def kanren(self,enemy):
enemy.hp-=15
print('【%s】砍了【%s】一刀'%(self.name,enemy.name))
def dazhao(self,enemy):
enemy.hp-=30
print('【%s】对【%s】使用天外飞仙,眩晕【%s】一回合'%(self.name,enemy.name,enemy.name))
def chiyao(self):
self.hp+=10
print('【%s】吃了一口药'%(self.name))
def __str__(self):
return '%s还剩下%s的血量'%(self.name,self.hp)
xmcx=Role('西门吹雪',300)
ygc=Role('叶孤城',300)
time.sleep(3)
print('决战紫禁之巅')
print('对战双方:')
print(xmcx.name,'生命:%s'%(xmcx.hp))
print(ygc.name,'生命:%s'%(ygc.hp))
print('战斗开始!')
time.sleep(3)
while True:
if xmcx.hp<=0 or ygc.hp<=0:
break
a=random.randint(1,9)
if a in [1,2,3]:
ygc.dazhao(xmcx)
print(ygc)
print(xmcx)
print('**************************************')
continue
else:
ygc.kanren(xmcx)
print(ygc)
print(xmcx)
print('**************************************')
xmcx.tong(ygc)
print(ygc)
print(xmcx)
print('**************************************')
xmcx.chiyao()
print(ygc)
print(xmcx)
print('**************************************')
time.sleep(1)
print('游戏结束....')
print('西门吹雪获胜')
|
|