代码之家  ›  专栏  ›  技术社区  ›  meanman211

我正在尝试制作一个简单的随机生成器,但我想在其中一个输出上使用if命令

  •  1
  • meanman211  · 技术社区  · 8 年前

    我只是想为我正在写的RPG制作一个随机怪物生成器。我正在使用Python 2.7。这很难解释,但我会尽力的。

    因此,我试图使它成为这样,如果“random.choice(number)”出现“has 1”变量,它将使用“random.choice(attachmentssingle)”而不是普通的“random.choice(attachments)”

    这是因为我不希望它打印出“有一条腿”或“有一个头”。相反,它会打印出“有一条腿”或“有一个头”。

    有没有办法做到这一点而不太复杂?(我对Python非常陌生,正在生病的时候努力学习。)

        import random
    
    attitude = ("An excited", "An angry", "A rabid", "A sadistic", "A depressed")
    nouns = ("dog", "cat", "rabbit", "snake", "bird")
    compose = ("composed of")
    material = ("flame.", "wood.", "ash.", "glass.", "flesh.", "metal.")
    it = ("It")
    attribute = ("has large mandibles,", "has a gaunt appearance,", "has no eyes or mouth,", "is invisible,", "breathes fire,", "screams endlessly,")
    word = ("and it")
    numbers = ("has 1", "has 2", "has 3", "has 4", "has 5", "has 6", "has 7", "has 8", "has 9", "has 10")
    attachments = ("arms", "legs", "tentacles", "heads", "mouths")
    attachmentssingle = ("arm", "leg", "tentacle", "head", "mouth")
    moreword = ("and it")
    features = ("has an unquenchable thirst for blood.", "wants to destroy all living creatures.", "is incredible lusty.", "wants to control the human race.", "has an interest in sentient life.", "hates silence.")
    
    print random.choice(attitude), random.choice(nouns), compose, random.choice(material),  it, random.choice(attribute), word, random.choice(numbers), random.choice(attachments), moreword, random.choice(features)
    
    
    if random.choice(numbers) == "has 1":
    print random.choice(attachmentssingle)
    
    input('Press ENTER to exit')
    

    编辑:我的代码需要一个缩进块,底部附近有“print random.choice(attachmentssingle)”。但是,如果“random.choice(numbers)”出现“has 1”,那么代码就可以正常工作,只是我试图用“random.choice(attachmentssingle)”替换“random.choice(attachments)”

    编辑2:我尝试了@user202729的建议,这就是我想到的:

    print random.choice(attachments) if random.choice(numbers) = "has 1" else print random.choice(attachmentssingle)
    

    然而,它似乎不起作用,因为它的语法错误,我尝试了几种不同的方式键入它,但它似乎不起作用。

    编辑3:@Patrick Artner一针见血,非常感谢你!我真的很感谢你们帮我解决这个问题!

    2 回复  |  直到 8 年前
        1
  •  1
  •   shayelk    8 年前

    可以像这样使用三元运算符:

    word = random.choice("and it has")
    number = random.choice("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
    attachment = random.choice("arm", "leg", "tentacle", "head", "mouth")
    attachment = attachment + ('' if number == "1" else 's')
    
        2
  •  0
  •   Patrick Artner    8 年前

    您可以使用一个怪物类来记住所有怪物参数,以供以后使用。您需要至少记住某个地方的附件数量,以便决定使用哪种附件。

    (非常基本)

    import random 
    
    class Monster(object):
    
        def __init__(self):
            self.attitude = random.choice(["An excited", "An angry", "A rabid",
                                           "A sadistic", "A depressed"])
    
            self.nouns = random.choice(["dog", "cat", "rabbit", "snake", "bird"])
            self.material = random.choice(["flame.", "wood.", "ash.", "glass.",
                                           "flesh.", "metal."])
    
            self.attribute = random.choice(["has large mandibles,", "has a gaunt appearance,", 
                                            "has no eyes or mouth,", "is invisible,", 
                                            "breathes fire,", "screams endlessly,"])
    
            self.numbers = random.choice(["has 1", "has 2", "has 3", "has 4", 
                                          "has 5", "has 6", "has 7", "has 8", 
                                           "has 9", "has 10"])
            # you use either one of the lists, dependingf on the result of self.numbers
            # this is the "ternary operator" that was mentioned in the comments
            self.attachments = random.choice(["arms", "legs", "tentacles", "heads",
                                              "mouths"]) if self.numbers != "has 1" else \
                               random.choice(["arm", "leg", "tentacle", "head", "mouth"])
    
            self.features = random.choice(["has an unquenchable thirst for blood.", 
                                           "wants to destroy all living creatures.", 
                                           "is incredible lusty.", 
                                           "wants to control the human race.",
                                           "has an interest in sentient life.", 
                                           "hates silence."])
    
    
        def __str__(self):
            return ' '.join([self.attitude, self.nouns, "composed of", self.material, "It",  
                             self.attribute, "and it",  self.numbers, self.attachments , 
                             "and it", self.features]) 
    
    # create 5 unique monsters
    M1 =  Monster()  
    M2 =  Monster()
    M3 =  Monster()
    M4 =  Monster()
    M5 =  Monster()
    
    # print the descriptions (`__str__()` - output) of each:
    print M1 
    print M2  
    print M3 
    print M4 
    print M5 
    

    输出(重新格式化):

    A rabid rabbit composed of flesh. It screams endlessly, and it has 
    3 heads and it is incredible lusty.
    
    A depressed dog composed of flame. It has a gaunt appearance, and it 
    has 6 legs and it has an unquenchable thirst for blood.
    
    A rabid bird composed of flesh. It is invisible, and it has 8 heads 
    and it wants to destroy all living creatures.
    
    A depressed snake composed of wood. It has a gaunt appearance, and it 
    has 4 mouths and it has an unquenchable thirst for blood.
    
    A depressed cat composed of ash. It is invisible, and it has 6 mouths 
    and it wants to control the human race.
    

    优点:如果他们也受到HP或攻击,您也可以将其“脚本化”到类实例中,并根据这些记忆值进行计算:

    print "You chop up one ",  M3.attachments, " of ", M3.attitude, M3.nouns
    

    输出:

    You chop up one  arm  of  An excited dog