I feel like this should work, but it doesn't! Help Me!


#1

I was trying to make a program take 10 root words like "spear" and "mace" and then put a random prefix infront of it, like "rusty" or "sharp". So, you do LootGenerate() and get "rusty spear", or "sharp mace" I feel like this should work, but it doesn't. Can anyone tell me why?

<do not remove the three backticks above>

def LootGenerate():
    rootword = randrange(1,10)
    rootwords = ["short-sword", "knife", "mace", "dagger", "spear", "broadsword", "pike", "war-axe", "staff", "club"]
    descriptorword = randrange(1,13)
    descriptorwords = ["golden", "sharp", "rusty", "deadly", "copper", "steel", "iron", "engraved", "bejeweled", "enchanted", "dark", "heavy", "light",]
    root = rootwords[rootword]
    descriptor = [descriptorwords[descriptorword]
    print descriptor + root
LootGenerate()

#2

this line:

descriptor = [descriptorwords[descriptorword]

not sure what the [ before descriptorwords is doing, it is causing the error


#3

Oh. Well, it still doesn't work, and I need to change my randrange from 0-12 and 0-9


#4

Traceback (most recent call last):
File "python", line 10, in
File "python", line 3, in LootGenerate
NameError: global name 'randrange' is not defined

is my error report thingy


#5

randrange is a function which isn't included by default, you need to include it:

from random import randrange

#6

Oh, ok thanks
Last night I got this code to work anyway, But that's nice to know :smiley:
import random
def LootGenerate():
rootword = random.randint(0, 9)
rootwords = ["short-sword", "knife", "mace", "dagger", "spear", "broadsword", "pike", "war-axe", "staff", "club"]
descriptorword = random.randint(0, 12)
descriptorwords = ["golden", "sharp", "rusty", "deadly", "copper", "steel", "iron", "engraved", "bejeweled", "enchanted", "dark", "heavy", "light",]
root = rootwords[rootword]
descriptor = descriptorwords[descriptorword]
print descriptor + " " + root
LootGenerate()


#7

now you are importing everything from random, you just need the one. But both methods will work :slight_smile: Good you got it to work