T.Blair
T.Blair

Reputation: 27

TypeError: 'in <string>' requires string as left operand, not method PYTHON

When trying to run my code, I am presented with a TypeError. It states: TypeError:'in'requires string as left operand, not method.

I am not sure what this means exactly, or how to fix it.

The code the error is created in is:

from tkinter import *
from tkinter import messagebox

def saveCustomer() :

    ClientID = ClientIDVar.get()
    ClientID = ClientID.ljust(50)
    Surname = SurnameVar.get()
    Surname = Surname.ljust(50)
    Forename = ForenameVar.get()
    Forename = Forename.ljust(50)
    Age = AgeVar.get()
    Age = Age.ljust(50)
    Gender = GenderVar.get()
    Gender = Gender.ljust(50)

    #lengthcheck
    ClientID = ClientIDVar.get()
    if len(ClientID) > 5 :
        messagebox.showerror("Error","Too many characters in ClientID")
        return
    Surname = SurnameVar.get()
    if len(Surname) > 20 :
        messagebox.showerror("Error","Too many characters in Surname")
        return
    Forename = ForenameVar.get()
    if len(Forename) > 15 :
        messagebox.showerror("Error","Too many characters in Forename")
        return
    Age = AgeVar.get()
    if len(Age) > 2 :
        messagebox.showerror("Error","Too many characters in Age")
        return
    Gender = GenderVar.get()
    if len(Gender) > 2 :
        messagebox.showerror("Error","Too many characters in Gender")
        return
    #presencecheck
    if ClientID == "":
        messagebox.showerror("Error","Please enter characters into ClientID")
        return
    if Surname == "":
        messagebox.showerror("Error","Please enter characters into Surname")
        return
    if Forename == "":
        messagebox.showerror("Error","Please enter characters into Forename")
        return
    if Age == "":
       messagebox.showerror("Error","Please enter characters into Age")
        return
    if Gender == "":
        messagebox.showerror("Error","Please enter characters into Gender")
        return
    #rangecheck
    if (int(Age) < 18) or (int(Age) > 99):
        messagebox.showerror("Error","Please enter an age between 18 and 99")
        return
    #typecheck
    if not ClientID.isnumeric():
        messagebox.showerror("Error","Please enter alphabetical characters and numerical values into ClientID")
        return
    if not Surname.isalpha():
        messagebox.showerror("Error","Please enter alphabetical characters into Surname")
        return
    if not Forename.isalpha():
        messagebox.showerror("Error","Please enter alphabetical characters into Forename")
        return
    if not Age.isnumeric():
        messagebox.showerror("Error","Please enter numerical values into Age")
        return
    if not Gender.isalpha():
        messagebox.showerror("Error","Please enter alphabetical characters and numerical values into Gender")
        return







    #open the file to append - if it's not there it'll be created
    fileObject = open("Customers.txt","a")
    # write to the file with a newline character at the end
    fileObject.write(ClientID + Surname + Forename + Age + Gender + "\n")
    fileObject.close()

    return

def searchCustomer() :
    Found = "N"
    ClientID = ClientIDVar.get
    # try opening the file for reading
    try:
        fileObject=open("Customers.txt","r")

    # if it's not there then say
    except IOError:
        messagebox.showerror("Error","No file to read")

    # if we did open it then let's carry on!
    else:
        while True:

            recordVar=fileObject.readline()
            # Python keeps reading till EOF then returns a blank
            if recordVar=="":
                fileObject.close()
                break

            if ClientID in recordVar[0:50] and not ClientID=="" :
                Found = "T"
                messagebox.showinfo("Information",str(RecordVar))
                break

    if Found == "F":
        messagebox.showerror("Error","No Customer found.")
    return



def makeWindow():
    #declared my globals here as this is the 1st routine called
    # the other routines have to be in front of this one as they get called by it
    # and the parser would get upset if they weren't there

    global ClientIDVar, SurnameVar, ForenameVar, AgeVar, GenderVar

    #here's my window
    win = Tk()
    win.wm_title("FitnessCentre")
    #split into two sections then further split into a grid
    frame1=Frame(win)
    frame1.pack()

    Label(frame1, text="Fitness Leisure Centre", font=("Helvetica 12             bold")).grid(row=0, column=0)

    Label(frame1, text="ClientID").grid(row=1, column=0, sticky=W)
    ClientIDVar=StringVar()
    title= Entry(frame1, textvariable=ClientIDVar)
    title.grid(row=1,column=1,sticky=W)

    Label(frame1, text="Surname").grid(row=2, column=0, sticky=W)
    SurnameVar=StringVar()
    genre= Entry(frame1, textvariable=SurnameVar)
    genre.grid(row=2,column=1,sticky=W)

    Label(frame1, text="Forename").grid(row=3, column=0, sticky=W)
    ForenameVar=StringVar()
    director= Entry(frame1, textvariable=ForenameVar)
    director.grid(row=3,column=1,sticky=W)

    Label(frame1, text="Age").grid(row=4, column=0, sticky=W)
    AgeVar=StringVar()
    leadactor= Entry(frame1, textvariable=AgeVar)
    leadactor.grid(row=4,column=1,sticky=W)

    Label(frame1, text="Gender").grid(row=5, column=0, sticky=W)
    GenderVar=StringVar()
    duration= Entry(frame1, textvariable=GenderVar)
    duration.grid(row=5,column=1,sticky=W)

    frame2 = Frame(win)
    frame2.pack()

    # build my buttons in the other frame then pack them side by side
    b1= Button(frame2, text=" Save ", command=saveCustomer)
    b2= Button(frame2, text=" Search ", command=searchCustomer)
    b1.pack(side=LEFT); b2.pack(side=LEFT)

    return win


#main program!
win = makeWindow()
win.mainloop()

The complete error message I received was:

Exception in Tkinter callback
Traceback (most recent call last):

if ClientID in recordVar[0:50] and not ClientID=="" :
TypeError: 'in <string>' requires string as left operand, not method

Upvotes: 0

Views: 2327

Answers (2)

ASL
ASL

Reputation: 283

Quoting from question:

TypeError: 'in' requires string as left operand, not method.

So somewhere you are using the in operator with a method on the left, not a string as expected. Had you posted the entire error/stack trace (as you should have done) we might have seen which line is causing the error. With a quick scan of your code I'd guess the error is here:

ClientID = ClientIDVar.get

Presumably you wanted to return the value of the ClientIDVar, not its get method (which is what you got). So it's a simple fix -- call the function properly:

ClientID = ClientIDVar.get()

Upvotes: 1

Denis Callau
Denis Callau

Reputation: 285

You can't use an method to test an in comparison, you have to compare with a string:

if ClientID in recordVar[0:50] and not ClientID=="" :

The error that you get are clearly explaining this to you:

TypeError. It states: TypeError:'in'requires string as left operand, not method.

Upvotes: 0

Related Questions