I'm writing part of a program that involves writing user generated information to a text file.

class MonsterStats:

  def __init__(self, name):
    self.__monName = name

  def setName(self, name):
    self.__monName  = name

  def fileWrite(self):
    monFile = open('MonsterFile.txt', 'w')
    monFile.write(self.__monName)
    monFile.close()

I wrote this to test it out:

import MonsterClass
name = raw_input('enter name: ')
monStats = MonsterClass.MonsterStats(name)
monStats.fileWrite()

The prompt comes up as it should but I get an error:

Traceback (most recent call last):
  File "D:/Python/MonsterMaker/testClass.py", line 7, in <module>
    monStats.fileWrite()
  File "D:/Python/MonsterMaker\MonsterClass.py", line 13, in fileWrite
    monFile.write(self.__monName)
AttributeError: 'tuple' object has no attribute 'write'

What is a 'tuple' object and how do I fix this?

Recommended Answers

All 7 Replies

I don't know how you could possibly be getting a tuple object... I used your code exactly and it worked just fine. A tuple is this:

a = (1, 2, 3, 4) # tuple with 4 elements
b = ('a', 'b')   # tuple with 2 elements

# tuples can be iterated over like lists
for item in a:
    print a

Don't know how you could possibly be getting a tuple in there.. Is that all your code?

That is very strange. That is the entirety of my code. You say it worked? Wrote your input to the file and everything?

EDIT: seems to have been a problem with my IDE. I restarted it and it works. I really should stop using IDLE. It's so bad >.<

I wrote your code in the one file like this and it worked

class MonsterStats:
  def __init__(self, name):
    self.__monName = name

  def setName(self, name):
    self.__monName  = name

  def fileWrite(self):
    monFile = open('MonsterFile.txt', 'w')
    monFile.write(self.__monName)
    monFile.close()

name = raw_input('enter name: ')
monStats = MonsterStats(name)
monStats.fileWrite()

Yes, it wrote my input to file. The code works. Maybe you have a typo in your code somewhere. But if you copy-pasted it to this forum then you have something seriously wrong with your python distro.

What version are you using?

def __init__(self, name):
self.__monName = name

monFile.write(self.__monName)

Somehow you are passing a tuple to the class. How do you call the class and what name are you entering? Also, add a print statement before the write
print type(self__monName), self.__monName
monFile.write(self.__monName)

It's 2.6 but it works now. I just restarted IDLE and it worked. BTW, anyone have a suggestion for a better IDE?

It's 2.6 but it works now. I just restarted IDLE and it worked. BTW, anyone have a suggestion for a better IDE?

I use Notepad ++ and set it up to be able to run my Python programs, verify their syntax and launch the PyCrust shell...

I highly recommend it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.