When I'm bored, I occasionally lurk this forum to see what fun coding projects everyone is working on. I enjoy world-building programs, but most people don't post their code, but rather screenshots of the the worlds they've made. I took a look at your code, but when you see a lines upon lines of nested if/else-if statements, it's almost a sure thing there's a better way to accomplish the same task. I don't know Python, but I know C++ and Java, so I thought this would be a fun way to learn a bit, exploring different ways to do the same thing and making code more concise in an unfamiliar environment. I assume you're learning Python yourself, so maybe it would help to see what I've done with your code.
I had to take a minute to analyse your if-else structure. It looks like you have three categories of keywords -- aggressive, religious, welcoming. Two keywords are picked from one category, and one is picked from a second category, but 'aggressive' and 'welcoming' are mutually exclusive.
I put all your keywords into a single array (I think Python calls them lists?) and by calculating a few numbers, I was able to do away with the whole if/elif structure. ~130 lines of code became ~20. If you plan on expanding your code to include more keywords and categories, it should be easier to do so by adding a few words to the list and changing a couple numbers here and there.
Code:
def civgen(n):
from random import randint
f = open('civ.txt', 'a')
keyword = ['agressive... ', 'territorial. ', 'crusading... ', 'colonizing.. ', 'suspicious.. ',
'religious... ', 'grandiose... ', 'inventive... ', 'carefree.... ', 'artistic.... ',
'welcoming... ', 'scholarly... ', 'innocent.... ', 'agricultural ', 'loyal....... ']
for i in range(n):
a = b = randint(0,2)
if (a == 1):
c = randint(0,1) * 2
else:
c = 1
a, b, c = a * 5, b * 5, c * 5
a, b, c = a + randint(0,4), b + randint(0,4), c + randint(0,4)
f.write(keyword[a])
f.write(keyword[b])
f.write(keyword[c])
for j in range(i, n - 1):
relation = randint(0, 9)
f.write(str(relation) + ',')
f.write('\n')
There are lots of peculiarities in Python regarding lists and variable assignments that I don't know about and that don't exist in Java or C, so there may yet be better ways of doing this.