【问题标题】:I am trying to make a simple random generator, but I want to use an if command on one of the outputs我正在尝试制作一个简单的随机生成器,但我想在其中一个输出上使用 if 命令
【发布时间】:2018-08-12 04:37:33
【问题描述】:

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

所以我试图做到这一点,如果“random.choice(numbers)”出现“has 1”变量,它使用“random.choice(attachmentssingle)”而不是正常的“random.choice(附件)”

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

有什么方法可以做到这一点而不会变得太复杂? (我对 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(attachmentssingle)”替换“random.choice(attachments)”,如果“random.choice(numbers)”出现“has 1”

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

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

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

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

【问题讨论】:

  • 您的代码目前有什么问题?它太长了?有效吗?
  • 在这种特殊情况下——只需修剪最后一个字符即可。
  • 我的代码需要一个缩进块,底部附近有“print random.choice(attachmentssingle)”。但除此之外,代码工作得很好,只是我试图用“random.choice(attachmentssingle)”替换“random.choice(attachments)”,如果“random.choice(numbers)”出现“has 1”
  • 如果是'is invisible.',你怎么知道所有这些事情?

标签: python python-2.7


【解决方案1】:

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

(非常基础)

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 或 Attacks,您也可以将它们“脚本化”到您的类实例中,并通过这些记住的值进行计算:

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

输出:

You chop up one  arm  of  An excited dog

【讨论】:

  • 这将是完美的,但它一直说第 22 行存在语法错误。
  • @meanman211 修复了它 - 它缺少一个 `` 以在将其重新格式化为 80 宽度后使行继续
  • 太棒了,现在它是完美的,我真的很感谢你帮我解决这个问题。在这里学习您的代码,我学到了很多东西!
【解决方案2】:

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

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')

【讨论】:

    猜你喜欢
    • 2020-10-27
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 2011-09-16
    • 2018-05-01
    相关资源
    最近更新 更多