python使用函数修改变量
我正在尝试为我正在学习的python基础课程编写一个函数。我们正处于这样一个时刻:我们作为一个团队加入,作为一个团队制定一个计划。我已指定每个成员将其部分作为函数编写,希望我可以调用每个函数来执行整个程序。自从我开始玩对象编程已经有一段时间了,这已经超出了类的要求,但我想尝试让它工作起来 我很难将变量传递到函数中,然后从函数中检索更改的变量 我曾尝试在多个网站上阅读并在这里进行深入搜索,但我遗漏了一些东西,任何帮助都将不胜感激 这是我尝试的第一个代码示例python使用函数修改变量,python,function,variables,Python,Function,Variables,我正在尝试为我正在学习的python基础课程编写一个函数。我们正处于这样一个时刻:我们作为一个团队加入,作为一个团队制定一个计划。我已指定每个成员将其部分作为函数编写,希望我可以调用每个函数来执行整个程序。自从我开始玩对象编程已经有一段时间了,这已经超出了类的要求,但我想尝试让它工作起来 我很难将变量传递到函数中,然后从函数中检索更改的变量 我曾尝试在多个网站上阅读并在这里进行深入搜索,但我遗漏了一些东西,任何帮助都将不胜感激 这是我尝试的第一个代码示例 import random MOBhp=
import random
MOBhp=100
def ATTACK(TYPEatck,MOBhp):
# global MOBhp
print ('the type attack chosen was ',TYPEatck,'the MOB has ',MOBhp)
if TYPEatck =='M'or TYPEatck =='m':
print ('the ',PLAYER,' used melee to attack',MOB)
MOBhp=MOBhp-10
elif TYPEatck =='R'or TYPEatck =='r':
print ('the ',PLAYER,' used range to attack',MOB)
MOBhp=MOBhp-5
else:
print ('please choose a valid attack')
print ('the MOB hitpoints are ',MOBhp)
return MOBhp;
PLAYER='HERO'
MOB='Dragon'
AC=12
while MOBhp > 0:
TYPEatck=random.choice('RM')
ATTACK(TYPEatck,MOBhp)
print('really the MOB hitpoints are ', MOBhp)
print(MOB,'was slain by ',PLAYER)
这给出了一个重复的结果,我必须用cntrl+c打破它
the type attack chosen was R the MOB has 100
the HERO used range to attack Dragon
the MOB hitpoints are 95
the type attack chosen was M the MOB has 100
the HERO used melee to attack Dragon
the MOB hitpoints are 90
the type attack chosen was R the MOB has 100
the HERO used range to attack Dragon
the MOB hitpoints are 95
好像我做了下面的事情
enter code here
#while MOBhp > 0:
TYPEatck=random.choice('RM')
ATTACK(TYPEatck,MOBhp)
print('really the MOB hitpoints are ', MOBhp)
print(MOB,'was slain by ',PLAYER)
我得到以下结果
the type attack chosen was R the MOB has 100
the HERO used range to attack Dragon
the MOB hitpoints are 95
really the MOB hitpoints are 100
Dragon was slain by HERO
我也尝试过使用全局变量,但似乎也无法实现这一点。现在,您正在丢弃计算结果。您必须将其存储并用作下一次计算的输入
while MOBhp > 0:
TYPEatck=random.choice('RM')
MOBhp = ATTACK(TYPEatck,MOBhp)
Mistermiagi的回答很有帮助。 如果要使用全局变量,可以这样使用:
import random
MOBhp=100
def ATTACK(TYPEatck, hp):
global MOBhp
MOBhp = hp
print('the type attack chosen was ',TYPEatck,'the MOB has ',MOBhp)
if TYPEatck == 'M' or TYPEatck == 'm':
print('the ',PLAYER,' used melee to attack',MOB)
MOBhp = MOBhp - 10
elif TYPEatck == 'R' or TYPEatck == 'r':
print('the ',PLAYER,' used range to attack',MOB)
MOBhp = MOBhp - 5
else:
print('please choose a valid attack')
print('the MOB hitpoints are ',MOBhp)
return MOBhp;
PLAYER = 'HERO'
MOB = 'Dragon'
AC = 12
while MOBhp > 0:
TYPEatck = random.choice('RM')
ATTACK(TYPEatck,MOBhp)
print('really the MOB hitpoints are ', MOBhp)
print(MOB,'was slain by ',PLAYER)
或者,您可以使用类:
import random
class Game():
def __init__(self, player, mob):
self.player = player
self.mob = mob
self.mobhp = 100
def ATTACK(self, TYPEatck):
print('the type attack chosen was ',TYPEatck,'the MOB has ',self.mobhp)
if TYPEatck == 'M' or TYPEatck == 'm':
print('the ',self.player,' used melee to attack',self.mob)
self.mobhp -= 10
elif TYPEatck == 'R' or TYPEatck == 'r':
print('the ',self.player,' used range to attack',self.mob)
self.mobhp -= 5
else:
print('please choose a valid attack')
print('the MOB hitpoints are ',self.mobhp)
return self.mobhp;
def get_MOBhp(self):
return self.mobhp
PLAYER = 'HERO'
MOB = 'Dragon'
AC = 12
game = Game(PLAYER, MOB)
while game.get_MOBhp() > 0:
TYPEatck = random.choice('RM')
game.ATTACK(TYPEatck)
print('really the MOB hitpoints are ', game.get_MOBhp())
print(MOB,'was slain by ',PLAYER)
谢谢大家的帮助!我找到了答案,能够完成我的程序
请修复缩进。否则Python代码就没用了。作为猜测,您似乎没有将
ATTACK
的返回值分配回MOBhp
。此外,在一些print
语句之后有不正确的空格,在或之前没有空格