Python-变量不会减去?

时间:2013-08-13 21:06:58

标签: python python-3.x

我正在尝试在Python(版本3.3.2)中创建一个简单的问答游戏,但无法弄清楚如何使表达式工作。下面看到的“健康”和“oppHealth”变量不会随着程序的运行而改变,或者至少字符串显示不会显示它们的变化。源代码:

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,并且健康显示字符串反映变化 - 每次战斗发生时。如果有人能帮助我,我会非常感激。如果我排除了可能对您有所帮助的任何信息,请与我们联系。

3 个答案:

答案 0 :(得分:3)

   health - 5
   oppHealth - 5

和类似的,不要实际修改任何东西,将减法保存回变量,改为使用-=运算符

health -= 5

或者你也可以说

health = health - 5

以上两个例子都达到了相同的效果。当您只是说health - 5时,实际上并没有将其保存在任何地方。

除此之外,您还需要在函数顶部指定global来修改这些值,否则您将收到错误。

def startBattle():
    global health
    global oppHealth
    # ... rest of function

此外,您不需要playStroppStr变量,您可以打印如下数值:

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()

虽然我也会摆脱所有的sleep。人们不喜欢像你想象的那样等待程序“做工作”,这只会让人们点击一下。

答案 1 :(得分:0)

阅读有关Python语法的更多信息。更改变量值的正确方法是,例如:

health = health - 5

答案 2 :(得分:0)

oppHealth - 5应写为

oppHealth = oppHealth - 5

您忘记保存计算结果