Python-变量Won';不减法?
我正在尝试用Python(3.3.2版)创建一个简单的问答游戏,但不知道如何使用表达式。下面看到的“health”和“oppHealth”变量不会随着程序的运行而改变,或者至少字符串显示不会显示它们的变化。源代码:Python-变量Won';不减法?,python,python-3.x,Python,Python 3.x,我正在尝试用Python(3.3.2版)创建一个简单的问答游戏,但不知道如何使用表达式。下面看到的“health”和“oppHealth”变量不会随着程序的运行而改变,或者至少字符串显示不会显示它们的变化。源代码: import time #Variables health = 30 oppHealth = 30 playStr = str(health) oppStr = str(oppHealth) def startBattle(): print() print('Yo
import time
#Variables
health = 30
oppHealth = 30
playStr = str(health)
oppStr = str(oppHealth)
def startBattle():
print()
print('You face off against your opponent.')
print()
print("Your health is " + playStr + ".")
print("Your opponent's health is " + oppStr + ".")
time.sleep(2)
print()
print('The opponent attacks with Fire!')
time.sleep(2)
print()
attack = input('How do you counter? Fire, Water, Electricity, or Ice?')
if attack == ('Fire'):
print("You're evenly matched! No damage is done!")
time.sleep(3)
startBattle()
elif attack == ('Water'):
print("Water beats fire! Your opponent takes 5 damage!")
oppHealth - 5
time.sleep(3)
startBattle()
elif attack == ('Electricity'):
print("You both damage each other!")
health - 5
oppHealth - 5
time.sleep(3)
startBattle()
elif attack == ('Ice'):
print("Fire beats ice! You take 5 damage.")
health - 5
time.sleep(3)
startBattle()
startBattle()
我只想让适当的健康变量在每次战斗发生时减少5,并让健康显示字符串反映变化。如果有人能帮我,我将不胜感激。如果我排除了任何可能有助于您帮助我的信息,请告诉我 阅读更多关于Python语法的内容。更改变量值的正确方法是,例如:
health = health - 5
oppHealth-5
应写成
oppHealth=oppHealth-5
你忘了保存计算结果了行
health - 5
oppHealth - 5
与此类似,不要实际修改任何内容,要将减法保存回变量中,请使用-=
运算符
health -= 5
或者你也可以说
health = health - 5
以上两个例子都得到了相同的结果。当你只是说health-5
时,你实际上并没有把它保存在任何地方
除此之外,您还需要在函数顶部指定global
,以修改这些值,否则将出现错误
def startBattle():
global health
global oppHealth
# ... rest of function
另外,您不需要playStr
和oppStr
变量,您可以这样打印数值:
print("Your health is", health, ".")
print("Your opponent's health is", oppHealth, ".")
这些实际上根本不需要是全局的,它们可以在函数中,处于循环中,我的程序版本如下:
#!/usr/bin/env python3
import time
def startBattle():
# set initial values of healths
health = 30
oppHealth = 30
print('You face off against your opponent.', end='\n\n')
while health > 0 and oppHealth > 0: # loop until someone's health is 0
print("Your health is {0}.".format(health))
print("Your opponent's health is {0}.".format(oppHealth), end='\n\n')
time.sleep(2)
print('The opponent attacks with Fire!', end='\n\n')
time.sleep(2)
print('How do you counter? Fire, Water, Electricity, or Ice?')
attack = input('>> ').strip().lower()
if attack == 'fire':
print("You're evenly matched! No damage is done!")
elif attack == 'water':
print("Water beats fire! Your opponent takes 5 damage!")
oppHealth -= 5
elif attack == 'electricity':
print("You both damage each other!")
health -= 5
oppHealth -= 5
elif attack == 'ice':
print("Fire beats ice! You take 5 damage!")
health -= 5
else:
print("Invalid attack choice")
time.sleep(3)
if health <= 0 and oppHealth <= 0:
print("Draw!")
if health <= 0:
print("You lose")
else:
print("You win!")
startBattle()
#/usr/bin/env蟒蛇3
导入时间
def startBattle():
#设置健康的初始值
健康=30
健康指数=30
打印('你面对你的对手',结束='\n\n')
当健康>0和oppHealth>0时:#循环直到某人的健康为0
打印(“您的健康状况为{0}”。.format(健康状况))
打印(“对手的生命值为{0}。”。格式(oppHealth),end='\n\n')
时间。睡眠(2)
print('对手用火攻击!',end='\n\n')
时间。睡眠(2)
打印(‘如何应对?火、水、电或冰?’)
攻击=输入('>>).strip().lower()
如果攻击=‘开火’:
打印(“你们是对等的!没有造成任何伤害!”)
elif攻击==‘水’:
打印(“水击火!你的对手受到5点伤害!”)
健康指数-=5
elif攻击==“电力”:
打印(“你们两个互相伤害!”)
健康-=5
健康指数-=5
elif攻击==“ice”:
打印(“火打冰!你受到5点伤害!”)
健康-=5
其他:
打印(“无效的攻击选择”)
时间。睡眠(3)
如果健康,谢谢你的回复。我之前尝试过表达“health=health-5”等等(应该提到):这没有任何区别。我刚才也尝试在函数中添加全局定义,它们也不会改变任何东西。@Chromashadow这是因为您没有更改playStr
和oppStr
变量来反映新的值,但是如果您使用我展示的print
,这将解决问题<代码>打印(“你的健康状况是{0}。”.format(health))
@Chromashadow你应该问一个问题,关于如何最好地解释火打在冰上,却输在水里,即使有电也不会对自己不利。我建议一个矩阵!