Loops 如何防止值在循环中重置?
我一直在尝试为我的学校项目建立一个健康系统,但不管monster_health在循环之外,它的价值不断重置,我和我的朋友们似乎无法解决这个问题 代码:Loops 如何防止值在循环中重置?,loops,python-3.7,integer-arithmetic,Loops,Python 3.7,Integer Arithmetic,我一直在尝试为我的学校项目建立一个健康系统,但不管monster_health在循环之外,它的价值不断重置,我和我的朋友们似乎无法解决这个问题 代码: monster_health = 100 while monster_health > 0: playeraction = input('Placeholder beast wants to fight you!!\n1) Basic Attack') print(monster_health) if playera
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
替换
if 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
请编辑问题,以包括实际输出和预期输出。
if playeraction == "1":
if int(playeraction) == 1:
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