为什么我的for循环不会从变量中减去?

时间:2018-06-12 21:50:53

标签: python python-3.x for-loop

我正在尝试制作一个超级简单的基于文本的拳击游戏作为练习练习。我希望每次通过循环都能打一拳并造成随机的伤害。每次打击都会击中对手的随机部分。 但是,伤害永远不会从原始数量中减去。 我哪里做错了?

这是我的代码和输出几轮。

#starting stats
nose = 100
jaw = 100
face = 100
head = 100
hp = nose + jaw + face + head
moral = 200
import random

#fight
for fight in range(10):
    if hp <= 0:
        print("DING DING DING\nKNOCK OUT\nYOU ARE THE WINNER!!!!!!")
    if hp <= 25:
        moral - 25
        print("He's losing moral!")
    if moral <=0:
        hp - 75
        print("He's about to KO!!!")
    if hp> 0:
        print("=========\nMORAL:{0}\nHP:{1}\nNOSE:{2}\nJAW:{3}\nFACE:{4}\nHEAD:{5}\n=========".format(moral, hp, nose, jaw, face, head))
        move = input('TYPE "P" TO THOW A PUNCH!!!')
        if move == "p" or "P" or "PUNCH":
            part = int(random.randint(1,4))
            damage = int(random.randint(1, 100))
            if part == 1:
                print("PUNCH TO THE NOSE!!!")
                nose - damage
            elif part == 2:
                print("PUNCH TO THE JAW!!!")
                jaw - damage
            elif part == 3:
                print("PUNCH TO THE FACE!!!")
                face - damage
            elif part == 4:
                print("PUNCH TO THE HEAD!!!")
                head - damage

输出:

=========
MORAL:200
HP:400
NOSE:100
JAW:100
FACE:100
HEAD:100
=========
TYPE "P" TO THOW A PUNCH!!!P
PUNCH TO THE HEAD!!!
=========
MORAL:200
HP:400
NOSE:100
JAW:100
FACE:100
HEAD:100
=========
TYPE "P" TO THOW A PUNCH!!!P
PUNCH TO THE HEAD!!!
=========
MORAL:200
HP:400
NOSE:100
JAW:100
FACE:100
HEAD:100
=========
TYPE "P" TO THOW A PUNCH!!!

3 个答案:

答案 0 :(得分:1)

你忽略了保存结果。试试这个:

    hp = hp - 75

更好的是,使用简写:

    hp -= 75

答案 1 :(得分:1)

hp - 75永远不会重置变量。考虑到这一点,hp始终是400的全局值。使用-=语法完成此操作:

hp -= 75

答案 2 :(得分:1)

moral - 25这样的陈述根本不做任何事情;他们从另一个值中减去一个值,然后丢弃结果。实际上,您需要将结果分配回变量:

moral = moral - 25

可以缩短为

moral -= 25