如何防止值在循环中重置?
How to keep a value from resetting in a loop?
我一直在尝试为我的学校项目设置一个健康系统,但无论 monster_health 处于循环之外,我的朋友和我似乎无法修复,该值一直在重置这个。
代码:
monster_health = 100
while monster_health > 0:
playeraction = input('Placeholder beast wants to fight you!!\n1) Basic Attack')
print(monster_health)
if playeraction == 1:
monster_health = monster_health-7
continue
if monster_health <= 0:
print('You killed the monster!! Gain nothing, this is a test you barbarian')
break
输出:
Placeholder beast wants to fight you!!
1) Basic Attack
100 #should print 97 but fails to
替换
if playeraction == 1:
和
if playeraction == "1":
或:
if int(playeraction) == 1:
因为您的输入是字符串格式而不是整数和 1 != "1"。
从你的代码来看,你在更新之前打印了怪物的生命值,所以它第一次是“100”(初始值)。
while monster_health > 0:
playeraction = input('Placeholder beast wants to fight you!!\n1) Basic Attack')
print(monster_health) # at this time, it is still 100
我一直在尝试为我的学校项目设置一个健康系统,但无论 monster_health 处于循环之外,我的朋友和我似乎无法修复,该值一直在重置这个。
代码:
monster_health = 100
while monster_health > 0:
playeraction = input('Placeholder beast wants to fight you!!\n1) Basic Attack')
print(monster_health)
if playeraction == 1:
monster_health = monster_health-7
continue
if monster_health <= 0:
print('You killed the monster!! Gain nothing, this is a test you barbarian')
break
输出:
Placeholder beast wants to fight you!!
1) Basic Attack
100 #should print 97 but fails to
替换
if playeraction == 1:
和
if playeraction == "1":
或:
if int(playeraction) == 1:
因为您的输入是字符串格式而不是整数和 1 != "1"。
从你的代码来看,你在更新之前打印了怪物的生命值,所以它第一次是“100”(初始值)。
while monster_health > 0:
playeraction = input('Placeholder beast wants to fight you!!\n1) Basic Attack')
print(monster_health) # at this time, it is still 100