检查的范围
ctr
你可以做的是:
全局定义ctr:
ctr = 0
#或者你想要的任何价值
然后在要重用的函数内部声明:
global ctr
因此,您的代码例如:
ctr = 0
class Player:
def __init__(self, name):
self.name = name
self.hp = 40
self.atk = 5
self.sp = 10
self.snk = 3 #FOR SNACKS ONLY
self.lvl = 1
self.exp = 0
def attack(self, other):
print("%s attacks %s for %d damage\n"%(self.name, other.name, self.a>
other.takeDamage(self.atk, self)
def takeDamage(self, dmg):
print("%s takes %d damage\n" % (self.name, dmg))
self.hp -= dmg
#if(self.hp <= 0):
# self.die()
#def die(self):
#print("%s died" %(self.name))
#print("-------GAMEOVER-------")
#print("You earned %d exp this game" %(self.exp))
def takeTurn(self, monsterArray):
global ctr #
print("%s HP:%d"%(self.name, self.hp))
print("Atk:%d"%(self.atk))
print("SP:%d"%(self.sp))
print()
#For every monster i, print information about it
for i in monsterArray:
#print information about Monster i
i.printInfo()
action = self.getAction()
#Player selected "Attack"
if(action == 1):
print("\nChoose target: ")
counter = 0
for i in monsterArray:
print("\n%d: " % (counter), end="")
i.printInfo()
counter += 1
userInput = int(input(">"))
self.attack(monsterArray[userInput])
if(monsterArray[userInput].hp <= 0):
monsterArray.pop(userInput)
if(action == 2):
print("\nYou are in Passive mode... - 1 dmg!\n")
self.hp -= 1
if(action == 3):
print("\nYou are heal up to 3+ SP")
self.hp += 8
ctr = ctr + 1 #count how many time used
def getAction(self):
global ctr #
while(1):
print("\nAvailable actions:")
print("1: Attack")
print("2: Passive Mode [-1 dmg]")
print("3: Snacks [%d out of 40]" % (ctr), end="")
try:
userInput = int(input(">"))
except ValueError:
print("Please enter a number")
continue
if(userInput < 1 or userInput > 3):
print("invalid action number")
continue
elif(userInput == 2 and self.sp < 5):
print("Loser! \n You are skipping yourself. Continue to figh>
continue
elif(userInput == 3 and self.hp< 40):
print("NO MORE.... Use your health leftover!")
continue
return userInput
没有
global
:
class Player:
def __init__(self, name):
self.name = name
self.hp = 40
self.atk = 5
self.sp = 10
self.snk = 3 #FOR SNACKS ONLY
self.lvl = 1
self.exp = 0
self.ctr = 0
def attack(self, other):
print("%s attacks %s for %d damage\n"%(self.name, other.name, self.a))
other.takeDamage(self.atk, self)
def takeDamage(self, dmg):
print("%s takes %d damage\n" % (self.name, dmg))
self.hp -= dmg
#if(self.hp <= 0):
# self.die()
#def die(self):
#print("%s died" %(self.name))
#print("-------GAMEOVER-------")
#print("You earned %d exp this game" %(self.exp))
def takeTurn(self, monsterArray):
print("%s HP:%d"%(self.name, self.hp))
print("Atk:%d"%(self.atk))
print("SP:%d"%(self.sp))
print()
#For every monster i, print information about it
for i in monsterArray:
#print information about Monster i
i.printInfo()
action = self.getAction()
#Player selected "Attack"
if(action == 1):
print("\nChoose target: ")
counter = 0
for i in monsterArray:
print("\n%d: " % (counter), end="")
i.printInfo()
counter += 1
userInput = int(input(">"))
self.attack(monsterArray[userInput])
if(monsterArray[userInput].hp <= 0):
monsterArray.pop(userInput)
if(action == 2):
print("\nYou are in Passive mode... - 1 dmg!\n")
self.hp -= 1
if(action == 3):
print("\nYou are heal up to 3+ SP")
self.hp += 8
self.ctr = self.ctr + 1 #count how many time used
def getAction(self):
while(1):
print("\nAvailable actions:")
print("1: Attack")
print("2: Passive Mode [-1 dmg]")
print("3: Snacks [%d out of 40]" % (self.ctr), end="")
try:
userInput = int(input(">"))
except ValueError:
print("Please enter a number")
continue
if(userInput < 1 or userInput > 3):
print("invalid action number")
continue
elif(userInput == 2 and self.sp < 5):
print("Loser! \n You are skipping yourself. Continue to figh")
continue
elif(userInput == 3 and self.hp< 40):
print("NO MORE.... Use your health leftover!")
continue
return userInput