While y'all are still on the subject of Python, could someone take a look at this thing?
I'm kinda sick today so I decided to be at least somewhat productive and whip up some sort of random name generator, however because I'm still a newbie I've run into a small (well, literally making it impossible to work) problem with it and I can't exactly tell how to get around it.
(The way the names are chosen would probably be seeded in whatever actual project I'd end up using this in, and it would not be printed to the console either but you get the idea.)
import random
"""Random post-apocalyptic name generator
Generates a random name out of concatenated strings pulled from thematic pools and returns it as a string
nameTypeId corresponds to what theme of the name is supposed to be:
0 - desert
1 - jungle
2 - tundra/iceland/glacier
3 - city
4 - spaceship
5 - hell
"""
def generateName(nameTypeId):
# pulls the appropriate name pool viat the getNamePool fuction as a list and stores them in the namePool dict
# name1 and name2 are randomly chosen from the frontPart and backPart lists and concatenaded to create the desired name
# sort of doesn't work though :s
if nameTypeId not in range(0,6):
return print("Yo, this ain't gonna work. bud.")
namePool = {"frontPart": getNamePool,"backPart": getNamePool}
nameFront = namePool["frontPart"]("frontPart",nameTypeId)
nameBack = namePool["backPart"]("backPart",nameTypeId)
name1 = random.choice(nameFront)
name2 = random.choice(nameBack)
return name1+" "+name2
def getNamePool(part,nameTypeId):
names = [
["Rock","Sand","Dust","Rocky","Sandy","Dusty","Flat","Dry","Storm","Cracked","Stone","Oasis","Sun","Skull","Drought","Red","Brown","Yellow","Orange","Hazel","Rolling"],
["Lands","Land","Valley","Plain","Plains","Rocks","Peaks","Pit","Canyon","Straits","Stones"],
["Toxic","Vine","Acid","Dark","Black","Sher","Muta","Savage","Wild","Great","Rotten","Green"],
["Jungle","Woods","Valley","Wood","Log","Trees","Bushes","Bush","Safari"],
["Cold","Frozen","Frost","Cool","White","Ice","Snow","Blizzard","Hail","Cryo","Blue","Chill","Freeze","Winter","Aurora"],
["Borealis","Wastes","Plains","Glacier","Wall"],
["Break","Kalash","Dead","Remington","Crushed","Steel","Iron","Fallen","Killer","Ruined","Nuke","Gray","Sky","Scrap","Bolt","Nut","Gun","New","Red","Blue","Grey","White","Black","Orange"],
["Town","City","York","Angeles","Peaks","Burg","Fort","Huts","Towers","Yard","Valley","Marino","Maria","Meltdown"],
["Red","Blue","Purple","Star","Great","Colossal","Space","Fallen","Green","Astral","Phantom","Mother"],
["Conqueror","Explorer","Empress","Odyssey","Scout","Comet","Storm","Hammer","Sword","Axe","Sun","Queen","Princess","Priestess","Mother"],
["Hot","Boiling","Scorched","Damned","Burned","Burning","Hell's","Melting","Molten","Lava","Magma","Sulfur","Fire","Brimstone","Abaddon","Soul","Devil","Devil's","Demon","Cursed"],
["Pit","Hole","Caves","Rocks","Mountain","Graves","Hive","Tunnels","Circle","Terror","Tremor","Quake","Dimension","Land"]
]
#Desert names
if part == "frontPart" and nameTypeId == 0:
return names[0]
elif nameTypeId == "backPart" and nameTypeId == 0:
return names[1]
#Jungle names
elif part == "frontPart" and nameTypeId == 1:
return names[2]
elif part == "backPart" and nameTypeId == 1:
return names[3]
#Tundra names
elif part == "frontPart" and nameTypeId == 2:
return names[4]
elif part == "backPart" and nameTypeId == 2:
return names[5]
#City names
elif part == "frontPart" and nameTypeId == 3:
return names[6]
elif part == "backPart" and nameTypeId == 3:
return names[7]
#Spaceship names
elif part == "frontPart" and nameTypeId == 4:
return names[8]
elif part == "backPart" and nameTypeId == 4:
return names[9]
#Hell names
elif part == "frontPart" and nameTypeId == 5:
return names[10]
elif part == "backPart" and nameTypeId == 5:
return names[11]
#Testing if the function works properly
print(generateName(7))
print(generateName(0))
print(generateName(1))
print(generateName(2))
print(generateName(3))
print(generateName(4))
print(generateName(5))
I thikn the problem is related to these lines here, at least if the call traceback is anything to go by.
namePool = {"frontPart": getNamePool,"backPart": getNamePool}
nameFront = namePool["frontPart"]("frontPart",nameTypeId)
nameBack = namePool["backPart"]("backPart",nameTypeId)
name1 = random.choice(nameFront)
name2 = random.choice(nameBack)
return name1+" "+name2
Traceback (most recent call last):
File "nameGen.py", line 74, in <module>
print(generateName(0))
File "nameGen.py", line 23, in generateName
name2 = random.choice(nameBack)
File "/usr/lib/python3.4/random.py", line 253, in choice
i = self._randbelow(len(seq))
TypeError: object of type 'NoneType' has no len()
I'm not exactly sure how combining functions as dictionary values and lists within lists works, as you can probably tell. :S
I kinda feel that the getNamePool function doesn't return the list properly since the random.choice() function is the root of the problem, so I'm not sure if I have to use a dict in place of that list with lists in it or if it's something else entirely.
I also tried using a randint instead of choice but it gave a similar kind of error.
(Plus there might be other issues for all I know but it doesn't seem to be the case).