Error message says: ' demo1' is NOT defined, but it actually has been defined

Hello all: After I wrote a program, and checked it over and over again, making sure nothing wrong. After I executed the program, the error messages showed up as the attached flie
. Can any experts pore over my program and tell me what mistake(s) I had made? Thanks for your help.

Look at the indentation level your definition of demo1 is at. Is that what you meant?

2 Likes

Thanks for your swift reply. My original intention was no indentation as the following image shown, but that was the 1st time the error message showed up. So I added the indentation to try to solve this problem which remained unsolved.

Your class is defined with lower case while in your call it is upper case.
Define with upper case:

class DemoClass: # It is customary to capitalize classes

demo1 = DemoClass()
1 Like

Sorry Paul, based on you advice, the problem remains.

The c should be capitalized:

You have it:

class Democlass:

It should be:

class DemoClass:
1 Like

Ok, we fixed that issue. The next issue is similar.

def Demo_class

but you reference it:

demo2.Demo_Class(
1 Like

Paul, can you please pore over the following image? Thanks.

Here is another example:

If I define a function as:

def sOme_FunC(x, y):
    return x * y

…, I then have to call it exactly how it has been spelled including letters that are either lower case or upper case:

sOme_FunC(5, 8)

Of course, we would not name a function like this but using it as an example.

Check your code using similar reasoning.

1 Like

All right. The problem is fully solved. One question, in my e-book, it’s

class demoClass. How come?

Paul, can you please answer the above question? Much obliged.

It’s generally accepted practice to capitalize classes.

Why the author chose to name the class as per function naming convention,
I suppose if it is your book, and you’re paying for it, you can do as you please.

But as I stated above, they are generally capitalized as per convention.

By the way, for functions and methods, you don’t capitalize the first letter.
This only applies to classes. So you can modify function/method names with
first letter being lower case in your code.

1 Like

Indeed, I am fully aware of the consistency of lower and upper cases in a program.

Paul, please take a look the attached file. In my e-book, it’s myClass, not MyClass, and my program runs without any problem on my laptop. Can you please comment?

According to Paul’s solution, it should be MyClass, not myClass. However, the program adopting myClass runs without any problem on my laptop. Can any experts comment, please?

Hi,

yes, it will work so long as the names match. Note that convention is NOT a rule. It is rather a formality or customary.

Reference PEP 8: PEP 8 – Style Guide for Python Code | peps.python.org

2 Likes

Roger. Thanks for your swift reply. One final question: we discussed why class demoClass did NOT work, and after your proposal, it should be DemoClass, then the problem was solved. So why demoClass didn’t work in the beginning and that’s what I still can NOT fathom. Your opinion?

Here is an example:

naming_variables

Notice that the spelling is correct. However, when I called it, I used a capital letter
S. Thus, every letter has to match, not just in spelling but in either upper case or’
lower case. If the case does not match either, it will be treated as a different variable.

So, to answer your original question, it did not work (it failed) because every letter did not match
in upper/lower case syntax.

1 Like

Roger. Now I fully understand. Thanks for your wise guidance, Paul.

1 Like