繁体   English   中英

Python-变量不会减去?

[英]Python- Variable Won't Subtract?

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

线条

   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 人们不喜欢像你想象的那样等待程序“做工作”,这只会让人们点击一下。

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

health = health - 5

oppHealth - 5应写为

oppHealth = oppHealth - 5

你忘了保存你的计算结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM