1

我正在尝试用 Python(3.3.2 版)创建一个简单的问答游戏,但不知道如何使表达式起作用。下面看到的“health”和“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- 并让健康显示字符串反映每次战斗发生时的变化。如果有人可以帮助我,我将不胜感激。如果我排除了任何可能对您有帮助的信息,请告诉我。

4

3 回答 3

3

线条

   health - 5
   oppHealth - 5

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

health -= 5

或者你也可以说

health = health - 5

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

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

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

此外,您不需要playStrandoppStr变量,您可以像这样打印数值:

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

虽然我也会摆脱所有的sleeps. 人们并不像你想象的那样喜欢等待程序“工作”,它只会让人们点击离开。

于 2013-08-13T21:10:51.217 回答
0

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

health = health - 5
于 2013-08-13T21:08:48.323 回答
0

oppHealth - 5应该写成

oppHealth = oppHealth - 5

您忘记保存计算结果

于 2013-08-13T21:09:06.990 回答