python3.2脚本挂起第三个函数

2024-06-16 11:49:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个非常简单的python脚本,它将两个字符放在一起,但是在运行脚本时,它执行脚本用来定义两个字符统计信息的前两个函数,但是当它到达第三个函数时,它就挂起了。在

代码如下:

#STAPS: Strength Toughness Agility Perception weapon Skill
#A comparative simulator
import random
#Functions used to define character parameters
#Character 1's parameter function

def char1():
    global s1
    global t1
    global a1
    global p1
    global dam1
    global dt1
    global dr1
    global ac1
    global ws1
    s1 = int(input("Char1's Strength? "))
    t1 = int(input("Char1's Toughness? "))
    a1 = int(input("Char1's Agility? "))
    p1 = int(input("Char1's Perception? "))
    dam1 = int(input("Char1's Damage? "))
    dt1 = int(input("Char1's Damage Threshold? "))
    dr1 = int(input("Char1's Damage Resistance? "))
    ac1 = int(input("Char1's Armor Class? "))
    ws1 = int(input("Char1's Weapon Skill? "))

#Character 2's paramter function
def char2():
    global s2
    global t2
    global a2
    global p2
    global dam2
    global dt2
    global dr2
    global ac2
    global ws2
    s2 = int(input("Char2's Strength? "))
    t2 = int(input("Char2's Toughness? "))
    a2 = int(input("Char2's Agility? "))
    p2 = int(input("Char2's Perception? "))
    dam2 = int(input("Char2's Damage? "))
    dt2 = int(input("Char2's Damage Threshold? "))
    dr2 = int(input("Char2's Damage Resistance? "))
    ac2 = int(input("Char2's Armor Class? "))
    ws2 = int(input("Char2's Weapon Skill? "))

#Main battle function. Ordo Xenos calls this "complex and easy to misuse"
#Jury-rigged way of getting names, why did I include them anyways?

def stapsbatt(c1n,c2n,hp1,hp2):
    while hp1 > 0 or hp2 > 0:
    #determines original raw acc
        char1rawacc = ws1 - ac2
    #if statement settles it to minimum 95% acc
    if char1rawacc > 95:
        char1rawacc = 95
    #random int used to determine whether it's a hit or not
    char1hitnum = random.randint(0, 100)
    if char1rawacc > char1hitnum:
        moddam1 = dam1 - dt2
        if moddam1 < 0:
            moddam1 = 0
        rawdam1 = moddam1 * (100 - dr2)
        hp2 = hp2 - rawdam1
    #Now we move on to doing char2's batt calcs
    char2rawacc = ws2 - ac1
    if char2rawacc > 95:
        char2rawacc = 95
    char2hitnum = random.randint(0, 100)
    if char2rawacc > char2hitnum:
        moddam2 = dam2 - dt1
        if moddam2 < 0:
            moddam2 = 0
        rawdam2 = moddam2 * (100 - dr1)
        hp1 = hp1 - rawdam2
    if hp1 == 0:
        print(c2n, "has won!")
    else:
        print(c1n, "has won!")
    char1()
    char2()
    stapsbatt("Character 1", "Character 2",400,30)
    input("Press enter to exit. ")

是的,这段代码是完全未经编辑的,我知道我的评论不是很好。在


Tags: toinputifrandomglobalintcharacterdamage
3条回答

代码中有一些问题导致它挂起。我建议您熟悉python调试器,例如pdb。这将允许您在运行时查看值,逐行逐行执行程序。在

撇开缩进问题不谈,以下是我发现的问题:

  1. 在while循环中,使用while hp1 > 0 and hp2 > 0作为条件。您不想在这里使用or,否则它会一直循环,直到两个字符都有<;0hp,所以您应该将其更改为and。在
  2. 当计算rawacc(对于这两个字符),您使用了if char1rawacc > 95,它实际上对char1rawacc强制执行一个最大值。你对char2rawacc做了同样的事情。将它们切换到if char1rawacc < 95if char2rawacc < 95。在
  3. 作为一个样式说明,如果您将此作为脚本执行,则应该将函数调用放在函数定义本身之外,一个好的方法是在脚本的末尾放置一个类似这样的块:
    if __name__ == "__main__":
        # the following only gets executed if the file is run as a script
        char1()
        char2()
        stapsbatt("Character 1", "Character 2", 400, 30)

希望这能让你走出无限循环!我用这些改变让游戏在我的电脑上运行。在

现在,正如Oz123提到的,你真的在滥用全局变量。相反,您应该研究如何创建和传递对象(参见9000的答案)。例如,您可以为您的角色定义一个类,并创建该类的两个实例(char1和char2),并传递给您的battle函数。这也将使您免于大量的冗余代码。这将需要一些重大的重组,但这是值得的。如果你在这方面遇到了问题,请随意提出一个新问题。在

你可能要找的问题是:

while hp1 > 0 or hp2 > 0:
    #determines original raw acc
    char1rawacc = ws1 - ac2

这个循环永远不会结束,因为无论你在里面做什么都不会改变它的状态。可能你想要一个if。在

剩下的:天哪。看到这个代码很痛苦。让我们做得更好一点。在

不要使用global,除非你有一个非常好的理由这样做。现在,把这看作是一个纪律问题;当你作为一个程序员进步时,你会明白为什么分离作用域很重要。在

使用函数来描述类似的事情一次。关键在于:去掉重复的部分,用名字代替,用新的“单词”让你的语言更接近你正在解决的问题。在

^{pr2}$

使用数据结构。在这种情况下,将一个角色的统计信息合并到一个实体中。在

# Use a dict to describe character stats
def get_character_description(name):
    description = {}
    description['name'] = name
    print "Describe character %s" % name
    description['strength'] = int(input("%s's strength?"))
    # input other values here...
    return description

char1 = get_character_description('Pikachu')
if char1['strength'] > 100: ...

了解类时,请考虑创建一个自定义类来描述字符:

class Character(object):
    def __init__(self, name):
        self.name = name

    def input(self):
        print "Let's define stats of %s" % self.name
        self.strength = int(input("Strength?"))
        # and so on

char1 = Character('J.C. Denton')
if char1.strength > 100: ...

之后,您的代码可能如下所示:

char1 = get_character_description('Liu Kang')
char2 = get_character_description('Sub Zero')
if char1.wins_over(char2):
   print char1.name, "has won"
else:
   print char2.name, "has won"

首先,注释必须与代码处于相同的缩进级别。在

相关问题 更多 >