0

I'm trying to work through python assignments because I already know java and C#, and managed to place out of the python class in my college with my AP Computer Science score.

This is a SetTitle function that I have created. The Write function has already been implemented in the given class.

class HTMLOutputFile:

def SetTitle( title ):
    if not str(title):
        return false
    else:
        Write("<TITLE>",title,"<TITLE>")
        return true

This file is calling my SetTitle method to make sure it works properly.

from htmloutputfile import *
import random

MyHTMLOutputFile = HTMLOutputFile()

if MyHTMLOutputFile.SetTitle(random.randint(1,100)):
    print('Error: SetTitle accepted non-string')
    exit(0)

if not MyHTMLOutputFile.SetTitle('My Title'):
    print('Error: SetTitle did not accept string')
    exit(0)

But, when I run it, I receive the error

if MyHTMLOutputFile.SetTitle(random.randint(1,100)):
TypeError: SetTitle() takes exactly 1 argument (2 given)

Do you guys have any ideas why random.randint(1,100) might be considered as two arguments instead of 1? I don't need a direct fix if you don't want to give it to me, but I'd like a point in the right direction.

Thanks.

Daniel Cole
  • 49
  • 1
  • 8

2 Answers2

3

change

def SetTitle( title ):

to

def SetTitle(self, title ):

Each class method must have the first parameter the instance the method is called on. It thinks it is two arguments because it automatically passes self to the function.

What is the purpose of self?

Community
  • 1
  • 1
marsh
  • 2,592
  • 5
  • 29
  • 53
  • Thank you! So in that case if the Write method was defined as Write(self,outstring) would i have to pass that in within the SetTitle method? – Daniel Cole Feb 26 '15 at 22:24
  • No, that is done automatically. Which is why it said there was 2 arguments. – marsh Feb 26 '15 at 22:45
0

randint is generating only one value, but since this is a method of the class, you need to use self as the first argument -

class HTMLOutputFile:

    def SetTitle(self, title):
        if not str(title):
            return false
        else:
            Write("<TITLE>",title,"<TITLE>")
            return true

Also note a couple of things - the method needs to be indented within the class. And python uses True and False (note the capitalization)

Shashank Agarwal
  • 2,769
  • 1
  • 22
  • 24