• Our friends from AlphaCentauri2.info are in need of technical assistance. If you have experience with the LAMP stack and some hours to spare, please help them out and post here.

Quick help with a syntax error

NotSoGood

Emperor
Joined
Jan 25, 2009
Messages
1,077
Location
Finland
I'm adding dynamic civ names into my mod. I managed to compile the DLL properly, but now I get a syntax error I can't fix. The line in red gives the syntax error when the game loads but for me it seems correct:
Spoiler :
Code:
	def newNameCiv(self, pPlayer):
		curDesc = pPlayer.getCivilizationDescription(0)
		curShort = pPlayer.getCivilizationShortDescription(0)
		curAdj = pPlayer.getCivilizationAdjective(0)
		
		newName = curDesc
		newShort = curShort
		newAdj = curAdj
		
		stateReligion = pPlayer.getStateReligion()
		
		if stateReligion == gc.getInfoTypeForString("RELIGION_COMMUNISM"):
			if (localText.getText("TXT_KEY_DCN_RUSSIAN_MATCH", ()) in curAdj):
				newName = "Soviet Union"
				newShort = "Soviet Union"
				newAdj = "Soviet"
			
			sSocRep = localText.getText("TXT_KEY_DCN_SOC_REP", ())
			sPeoplesRep = localText.getText("TXT_KEY_DCN_PEOPLES_REP", ())

			[COLOR="Red"]elif (pPlayer.getCivics(gc.getInfoTypeForString("CIVICOPTION_LEGAL")) == gc.getInfoTypeForString("CIVIC_VASSALAGE")):[/COLOR]
				if (50 > game.getSorenRandNum(100,'Bob')):
					newName = sSocRep + curShort
				else :
					newName = sPeoplesRep + curShort
			else:
				newName = sPeoplesRep + curShort
		
		elif stateReligion == gc.getInfoTypeForString("RELIGION_DEMOCRACY"):
			if (pPlayer.getNumCities() == 1):
				newName = curAdj + localText.getText("TXT_KEY_DCN_REPUBLIC_OF", ()) + CvUtil.convertToStr(pPlayer.getCapitalCity().getName())
			elif (50 > game.getSorenRandNum(100,'Bob')):
				newName = localText.getText("TXT_KEY_DCN_REPUBLIC", ())%(curAdj)
			else:
				newName = localText.getText("TXT_KEY_DCN_THE_REPUBLIC_OF", ()) + curShort
			if (pPlayer.isCivic(gc.getInfoTypeForString("CIVIC_FREE_SPEACH")) and pPlayer.isCivic(gc.getInfoTypeForString("CIVIC_EMANCIPATION"))):
				newName = localText.getText("TXT_KEY_DCN_FREE", ()) + ' ' + newName
				
		
		return [newName, newShort, newAdj]
I've just started writing the python code so it might be kinda buggy.
I appreciate all help spotting the error.
 
Python is very picky about indentation. You have two lines after the "if" which are at the same indentation level. So, you cannot put an "elif" after that. If the two lines after the "if" are supposed to be executed only when the "if" triggers, then indent them one more tabstop.

I have never understood why the python authors feel indentation is "easier to understand" compared to scoping syntax like curly braces or semicolons.
 
Python is very picky about indentation. You have two lines after the "if" which are at the same indentation level. So, you cannot put an "elif" after that. If the two lines after the "if" are supposed to be executed only when the "if" triggers, then indent them one more tabstop.

I have never understood why the python authors feel indentation is "easier to understand" compared to scoping syntax like curly braces or semicolons.

So do you mean that if I move those two lines before the first if, it'll work?
I thought python was picky about indentation but this is way more than I thought.
Thanks for help.
 
I thought my description was clear, but perhaps not. Your code, with my colors:
Code:
[color="green"]if (localText.getText("TXT_KEY_DCN_RUSSIAN_MATCH", ()) in curAdj):[/color]
	newName = "Soviet Union"
	newShort = "Soviet Union"
	newAdj = "Soviet"
[COLOR="Red"]sSocRep = localText.getText("TXT_KEY_DCN_SOC_REP", ())
sPeoplesRep = localText.getText("TXT_KEY_DCN_PEOPLES_REP", ())[/color]
[color="blue"]elif (pPlayer.getCivics(gc.getInfoTypeForString("CIVICOPTION_LEGAL")) == gc.getInfoTypeForString("CIVIC_VASSALAGE")):[/color]
I suppose you expect the blue "elif" to be associated with the green "if". But, the red lines are at the same indentation level. This breaks the association of the "if" with the "elif". I assume that the red lines are only supposed to be executed when the "if" is true. So indent the two red lines by one tabstop. If that is not your intention, when are the red lines supposed to be executed?
 
Since sSocRep and sPeoplesRep are used only in the elif and else blocks, I assume you want those lines only to be run when entering those blocks, but you don't want them to run if the first if block triggers. Your logic is sound enough, you just need to use one more level of brancing:

Add an else and put the getText() calls, elseif and else clauses within it. And change the elseif to if because it is now starting a new if chain.

Code:
		if stateReligion == gc.getInfoTypeForString("RELIGION_COMMUNISM"):
			if (localText.getText("TXT_KEY_DCN_RUSSIAN_MATCH", ()) in curAdj):
				newName = "Soviet Union"
				newShort = "Soviet Union"
				newAdj = "Soviet"
			
			[B][COLOR="Red"]else:[/COLOR][/B]
				sSocRep = localText.getText("TXT_KEY_DCN_SOC_REP", ())
				sPeoplesRep = localText.getText("TXT_KEY_DCN_PEOPLES_REP", ())

				[B][COLOR="Red"]if[/COLOR][/B] (pPlayer.getCivics(gc.getInfoTypeForString("CIVICOPTION_LEGAL")) == gc.getInfoTypeForString("CIVIC_VASSALAGE")):
					if (50 > game.getSorenRandNum(100,'Bob')):
						newName = sSocRep + curShort
					else :
						newName = sPeoplesRep + curShort
				else:
					newName = sPeoplesRep + curShort
 
Every other language I know works the same way: C, C++, PHP, Perl, Java, etc. I've never seen a language that allows a disconnected else-if like that.
 
Back
Top Bottom