Python problem - writing random string to file

I keep getting the syntax error - TypeError: 'tuple' object is

Page 1 of 1

1 Replies - 3183 Views - Last Post: 19 October 2008 - 03:21 PM Rate Topic: -----

#1 Tama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-October 08

Python problem - writing random string to file

Posted 19 October 2008 - 03:06 PM

I'm trying to write randomly generated Artificial Intelligence files (for Age of Empires II: AoK) with Python. The problem is that I get the error:
TypeError: 'tuple' object is not callable
When I try to write a randomly selected string from an array to a file. This is my code:

import os.path
import random

version = 1
relOp = (">", "=", "<")
facts = ("true", "false", "attack-soldier-count", "attack-warboat-count")
while version < 30:
	if not os.path.exists("Recruits"):
		print "Recruits folder does not exist."
		print "Creating recruits folder."
		os.mkdir("Recruits")

	version = 1
	spawnPath = "C:\Documents and Settings\User\Desktop\Desktop\Tama\Genetic Algorithms\AoK\Recruits\\recruit" + str(version) + ".py"
	while os.path.exists(spawnPath):
		version += 1
		spawnPath = "C:\Documents and Settings\User\Desktop\Desktop\Tama\Genetic Algorithms\AoK\Recruits\\recruit" + str(version) + ".py"
	fileHandle = open(spawnPath, "w")
	maximumVillagers = random.randint(5,150)
	attackSoldierAmount = random.randint(5,150)
	fileHandle.write("(defrule\n")
	fileHandle.write("	(unit-type-count-total villager < " + str(maximumVillagers) + ")\n")
	fileHandle.write("	(can-train villager)\n")
	fileHandle.write("=>")
	fileHandle.write("	(train villager)\n")
	fileHandle.write(")\n")
	
	fileHandle.write("(defrule\n")
	fact = facts(random.randint(1,4))
	relOpCur = relOp(random.randint(1,3))
	fileHandle.write("	" + fact + relOpCur + str(attackSoldierAmount) + ")\n")
	fileHandle.write("	(can-train villager)\n")
	fileHandle.write("=>")
	fileHandle.write("	(train villager)\n")
	fileHandle.write(")\n")

	fileHandle.close()



Any help would be greatly appreciated. =D

This post has been edited by Tama: 19 October 2008 - 03:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Python problem - writing random string to file

#2 Stutzbach  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 06-July 08

Re: Python problem - writing random string to file

Posted 19 October 2008 - 03:21 PM

View PostTama, on 19 Oct, 2008 - 04:06 PM, said:

I'm trying to write randomly generated Artificial Intelligence files (for Age of Empires II: AoK) with Python. The problem is that I get the error:
TypeError: 'tuple' object is not callable


In the future, it would be helpful if you posted which line was generating the error. I suspect it's this one:

fact = facts(random.randint(1,4))
relOpCur = relOp(random.randint(1,3))

which should be:
fact = facts[random.randint(1,4)]
relOpCur = relOp[random.randint(1,3)]


Also, since tuples and list are indexed starting at 0 in Python, I suspect the calls to randint should have 0 as their first parameter instead of 1.

Hope that helps,
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1