Jubayre Jub
Jubayre Jub

Reputation: 1

class and identifier python

I'm having trouble with my program, I'm following the following instructions which is everything that was told from me.

The class shall contain: 1.A data field keywordList with the list of all Python keywords; the data field shall be initialized from keyword.kwlist.

2.A method named addKeyword(kw) that shall add another keyword kw to the list of keywords; if kw is already on the list, the method shall do nothing.

3.A method named isLegalId(name) that shall return True if name is a legal Python identifier (not a keyword and follows the naming rules) and False, otherwise.

The test program shall:

1.Create an object of type PythonIdentifier.

2.Report if the following words are legal Python identifiers: CMPSC, yield, who, 3letters, 132, __.

3.Add a new "keyword" __ to the object.

4.Report if the word __ is a legal Python identifier.

import keyword
class Pythonidentifier:
    def __init__(self,keywordlist):
       keywordlist=keyword.kwlist
       self.keywordlist= keywordlist
    def getgetkeywordList(self):
        return self.keywordlist
    def addkeyword(self,kw):
        if kw not in self.getkeywordList:
            self.getkeyword.append(kw)
        return
    def isLegalId(name):
        Truth=name.isidentifier
        return Truth
def main():
    """
    """
    try:    
        name=["CMPSC", "yield", "_who_", "3letters", "132", "__"]
        for i in name:
            print((name[i]))
            print("Input a keyword")
            newkeyword=input(">")
            if "".isLegalId(newkeyword)==True:    
                "".addkeyword(newkeyword)
            else:
                print("The keyword suggested isn't a proper identifier, please try again.")
            print("Here is the list of identifiers.")
            print()
    except:
        print("Something went wrong!")

    return
main()

Every time I run the program I get the except statement ( the error message)

Upvotes: 0

Views: 141

Answers (2)

Shawn
Shawn

Reputation: 1943

There are multiple things going wrong in your code. I will point out some mistakes to you since it seems to be a homework:

for i in name:
    print((name[i]))

In this loop, i will contain the list's values, it's not an index.

if "".isLegalId(newkeyword)==True:    
    "".addkeyword(newkeyword)

Here, you're trying to access the methods you created for the Pythonidentifier class but through a string object (which is not of type Pythonidentifier, see your task 1.)

Some mistakes in your class:

getgetkeywordList(self):

  • I believe you wanted to call it getkeywordList

in addkeyword(self,kw):

  • self.getkeywordList is not a valid function call!
  • self.getkeyword.append(kw): getkeyword is a method, what do you want to append the value to?

isLegalId(name)

  • no self argument?
  • name.isidentifier again is not a function call and also probably not the functionality you are looking for.

Upvotes: 1

AlokThakur
AlokThakur

Reputation: 3741

for i in name: print((name[i])) This is causing exception, your i is name not the index, change it as

for i in name:
            print(i)

Upvotes: 1

Related Questions