【发布时间】: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