1

I am just trying to make a random monster generator for an RPG I am writing. I am using Python 2.7. It's difficult to explain but I will try.

So I am trying to make it so that if "random.choice(numbers)" comes up with the "has 1" variable, it uses "random.choice(attachmentssingle)" instead of the normal "random.choice(attachments)"

This is because I don't want it to print out "has 1 legs" or "has 1 heads". Instead it would print out "has 1 leg" or "has 1 head".

Is there any way to do this without getting too complicated? (I'm very new to Python and am trying to learn it while I'm sick.)

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

EDIT: My code expected an indented block with "print random.choice(attachmentssingle)" near the bottom. But otherwise the code works just fine, it's just that I'm trying to replace "random.choice(attachments)" with "random.choice(attachmentssingle)" if "random.choice(numbers)" comes up with "has 1"

EDIT 2: I tried @user202729's suggestion and this is what I came up with:

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

However it doesn't seem to be working as it's the wrong syntax, and I've tried several different ways of typing it out but it doesn't seem to be working.

EDIT 3: @Patrick Artner hit the nail on the head, thanks a lot man! I really appreciate all you guys helping me out with this!

meanman211
  • 13
  • 3
  • 2
    And what's the problem with your code currently? It's too long? Is it working? – user202729 Mar 04 '18 at 11:37
  • 1
    [Does this help you?](https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator) – user202729 Mar 04 '18 at 11:39
  • In this particular case -- just trim the last character suffices. – user202729 Mar 04 '18 at 11:39
  • My code expected an indented block with "print random.choice(attachmentssingle)" near the bottom. But otherwise the code works just fine, it's just that I'm trying to replace "random.choice(attachments)" with "random.choice(attachmentssingle)" if "random.choice(numbers)" comes up with "has 1" – meanman211 Mar 04 '18 at 11:43
  • How do you know all those things if it `'is invisible.'` ? – Patrick Artner Mar 04 '18 at 11:44
  • For this simple case, this might be an overkill, but it is worth to mention a library for pluralizing and much more - [inflection](https://pypi.python.org/pypi/inflection) – Eugene K Mar 04 '18 at 11:46
  • http://dpaste.com/0B1BX6W – 3141 Mar 04 '18 at 11:52
  • @3141 We dont want links to code in questions , we do not like "link only answers" - how do you think a "link only comment" - to code - will fair? – Patrick Artner Mar 04 '18 at 12:14
  • Sorry @PatrickArtner, it was more of a passing suggestion than an answer. – 3141 Mar 04 '18 at 19:57

2 Answers2

1

You can use the trinary operator like this:

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')
shayelk
  • 1,606
  • 1
  • 14
  • 32
0

You could use a monster class that remebers all the monster parameters for later use. YOu need to remember at least the number of appendages somewhere, so you can decide on which kind of attachments you use.

(very basic)

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 

Output (reformatted):

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.

Advantage: If they got HP or Attacks as well, you can "script" them into your class instance as well and compute by those rememberd values:

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

Output:

You chop up one  arm  of  An excited dog
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69