不停止打印

时间:2019-11-28 01:18:06

标签: python python-3.x

我正在进行一场战斗,据我所知,几乎所有事情都在起作用。攻击造成适当的伤害,并且玩家和敌人拥有正确的生命值,所有物品都起作用。但是,当您获胜时,它将永远重复文本“您获胜”,直到您强行结束该程序。如果运行状况恰好为0,也不会发生这种情况。我认为可能是问题的代码是...

def enemyHP():
    global enemyhealth
    global playerhealth
    if enemyhealth and playerhealth >0:
        global dmg
        global alcohol
        if alcohol==1:
            miss=random.randint(0,2)
            print("The alcohol makes you more furious...")
            if miss==0:
                dmg=0
            else:
                dmg=dmg*2
        enemyhealth=enemyhealth-dmg
        if enemyhealth<=0:
            print("you won")
            exit()
        else:
            print("you did",dmg,"damage")
            print("enemy health is:",enemyhealth)

我试图找到一种更好的方法来使用exit()quit()sys.exit()结束代码。他们都没有工作,我想知道是否有办法解决这个问题。预先感谢

从我可以简化的角度来看,完整的代码是这样

backpacklist=["sword","hand"]    
enemyhealth=100
playerhealth=100
def enemyHP():
    global enemyhealth
    global playerhealth
    if enemyhealth and playerhealth >0:
        global dmg
        enemyhealth=enemyhealth-dmg
        if enemyhealth<=0:
            print("you won")
            exit
        else:
            print("you did",dmg,"damage,","enemy health is:",enemyhealth)
    return enemyhealth
def playerattack():
    global playerhealth
    global enemyhealth
    if playerhealth and enemyhealth >0:
        print(backpacklist)
        global dmg
        help1=0
        whatweapon=input("what weapon?:")
        while backpacklist[help1]!=whatweapon:
            help1=help1+1
        if backpacklist[help1]==whatweapon:
            if backpacklist[help1]=="hand": #to quickly kill
                dmg=1000
            elif backpacklist[help1]=="sword":
                dmg=20
    return dmg,playerhealth
def playerHP(enemydmg):
    global enemyhealth
    global playerhealth
    if playerhealth and enemyhealth >0:
        playerhealth=playerhealth-enemydmg
        if playerhealth<=0:
            print("you lose")
            exit
        else:
            print("your health is:",playerhealth)
    return playerhealth
def fight1(enemydmg):
    while enemyhealth and playerhealth>0:
        global dmg
        dmg=0
        playerattack()
        enemyHP()
        playerHP(enemydmg)
enemydmg=10    
if enemyhealth and playerhealth >0:
    fight1(enemydmg)

好吧,在敌人的生命值低于0以后,我已经设法通过将敌人的HP设置为0来解决此问题。我不知道为什么会起作用,但是如果起作用了。

2 个答案:

答案 0 :(得分:2)

这里是代码的清理版本,可以很好地运行而不会像您提到的那样循环:

import random, sys

def enemyHP():
    global enemyhealth, playerhealth, dmg, alcohol
    if enemyhealth > 0 and playerhealth > 0:
        if alcohol == 1:
            miss = random.randint(0,2)
            print("The alcohol makes you more furious...")
            dmg = 0 if miss == 0 else dmg*2
        enemyhealth -= dmg
        if enemyhealth <= 0:
            print("you won")
            sys.exit()
        else:
            print("you did {} damage \nenemy health is: {}".format(dmg, enemyhealth))

# Your variables
enemyhealth = 1
playerhealth = 2
dmg = 1
alcohol = 1

# Call the function
enemyHP()

示例输出:

The alcohol makes you more furious...
you did 0 damage 
enemy health is: 1

这是基于上面的变量分配

答案 1 :(得分:1)

首先,我建议在exit()或quit()上使用sys.exit(),看看Here为什么这通常是个好主意。我在这里没有足够的信息来说明为什么sys.exit()不会退出该程序,但是我建议您看看在哪里调用该函数并进行更改。

例如,假设您正在某种形式的循环中运行此函数,每次战斗中每回合都会调用此函数,但是与其强迫退出程序中间功能,不如让它退出程序中间功能,有时最好还是让程序退出在满足首选状态时继续执行,因此,例如,如果敌人还剩下生命,则函数可以返回False,如果敌人死了,则返回True,如果函数返回True,甚至更好,则循环结束只需返回敌人的健康状况,并弄清楚您是在功能之外赢了还是输了。