Python中文
首页
教程
问答
标签
搜索
登录
注册
定义要在已定义的PYTHON中运行的全局变量
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>因此,我正在为学校作业编写一个基于RPG文本的游戏,尽管我在为玩家健康创建一个全局变量方面遇到了困难,因为我已经在一个定义的函数中编写了战斗逻辑。目前,每次通过函数播放时,它都会将playerHealth设置为100,尽管我希望它使playerHealth等于上次修改时的值。例如:如果我在一场战斗中失去了10点生命值,使我的生命值达到90,那么下次我战斗时它将被设置为90点生命值</p> <pre><code>def battle(): playerHealth = 100 enemyHealth = 20 while enemyHealth > 0: attack = input('Melee or Ranged Attack?\n> ').lower() if attack[0] == 'm': chance = random.randint(0,2) if chance == 0: print('***********') print('You missed!') print('***********') print('You receive 4 damage') playerHealth -= 4 print('') print('Player Health = ' + str(playerHealth)) else: print('You hit the enemy, also causing damage to yourself') print('Enemy receives 3 damage') print('Player receives 1 damage') enemyHealth -= 4 playerHealth -= 1 print('') print('Enemy Health = ' + str(enemyHealth)) print('Player Health = ' + str(playerHealth)) elif attack[0] == 'r': chance = random.randint(0,2) if chance == 0: print('***********') print('You missed!') print('***********') print('The enemy attacks you causing 3 damage') playerHealth -= 3 print('') print('Player Health = ' + str(playerHealth)) else: print('-----------') print('Direct hit!') print('-----------') print('Enemy receives 2 damage') enemyHealth -= 2 print('') print('Enemy Health = ' + str(enemyHealth)) print('---------------------') print('You killed the enemy!') print('---------------------') </code></pre> <p>所以后来我会用:</p> <pre><code>if "enemy" in deadZone[currentArea1]: battle() </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>在函数外部初始化它,将它传递给函数,并让函数返回它</p> <pre><code>def battle(playerHealth): ... return playerHealth ... if "enemy" in deadZone[currentArea1]: playerHealth = battle(playerHealth) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何合并多个PDF文件?
9 回答
如何合并多个xarray数据变量及其坐标?
8 回答
如何合并多个列中具有重复值的行
7 回答
如何合并多个唯一id
7 回答
如何合并多个图纸并使用图纸名称的名称重命名列名?
4 回答
如何合并多个字典并添加同一个键的值?(Python)
1 回答
如何合并多个搜索结果文件(pkl)以将它们全部打印在一起?
1 回答
如何合并多个数据帧
8 回答
如何合并多个数据帧并使用Pandas为假人添加列?
9 回答
如何合并多个数据帧并按时间戳排序
9 回答
如何合并多个数据帧的列表并用另一个lis标记每列
7 回答
如何合并多个数据框中的列
9 回答
如何合并多个文件?
7 回答
如何合并多个查询集?
1 回答
如何合并多个绘图?
9 回答
如何合并多个词典
8 回答
如何合并多个输入数据集(数据帧)?
7 回答
如何合并多条记录中拆分的文本行
10 回答
如何合并多索引列datafram
5 回答
如何合并多级(即多索引)数据帧?
2 回答