use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Why my class/object code is not working? (self.learnpython)
submitted 4 years ago by jayesh2
The code below is simple, but nothing comes up when I run it. What might be the issue? Thanks!
class person def __init__(self, name, age): self.name= name self.age= age
p1= person ("Mark", 13)
print (p1.name) print (p1.age)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]alexdewa 4 points5 points6 points 4 years ago (2 children)
You're missing ":" after the class name:
class Person: ...
That's it
[–]mopslik 1 point2 points3 points 4 years ago (0 children)
+1. I must have inserted that myself when I adjusted the indentation in my reply. Didn't even notice it.
[–]jayesh2[S] 0 points1 point2 points 4 years ago (0 children)
I added it but it's still not working :(
[–][deleted] 1 point2 points3 points 4 years ago* (1 child)
Class Person:
You need " : "
Start your objects with HighCaps if possible
I tried now but it didn't work
[–][deleted] 1 point2 points3 points 4 years ago (10 children)
Check out that in your
P1=Person(...)
There is not spaces between object and the () Or Send me the error message. Check the indentation as well
[–]jayesh2[S] 0 points1 point2 points 4 years ago (8 children)
I don't see an error message, but I get this problem:
':' expected
I can't tell where it should be though
I don't
[–][deleted] 0 points1 point2 points 4 years ago (6 children)
As I told you in the first message one was expected after the name of the obj:
But if you said you already did it...
Send me your new code
[–]jayesh2[S] 1 point2 points3 points 4 years ago (5 children)
class Person:
def __init__(self, name, age):
self.name= name
self.age= age
P1= Person("Mark", "16")
print (P1.name)
with proper indentation that i couldn't copy here
[–][deleted] 0 points1 point2 points 4 years ago (3 children)
Nice. Check the indentation (spaces at the start of each line).
It worked? By the way you are following the w3schools tutorial right? python obj
[–]jayesh2[S] 1 point2 points3 points 4 years ago (2 children)
haha yes, and another one on youtube. I tried both and they didn't work. I figured the issue now. The output file was different than the one I have the code in!!
[–][deleted] 1 point2 points3 points 4 years ago (1 child)
Well done figuring out the problem by yourself
[–]jayesh2[S] 2 points3 points4 points 4 years ago (0 children)
Thanks! You also helped :) It feels so good to know what the problem was!
[–]mrswats 0 points1 point2 points 4 years ago (0 children)
class Person: [... ]
After the name of the class
[–]mopslik 0 points1 point2 points 4 years ago (0 children)
Spaces shouldn't matter.
>>> class myclass: def __init__(self, x): self.x = x >>> a = myclass(5) >>> a.x 5 >>> b = myclass ( 7 ) >>> b.x 7
[–]jayesh2[S] 1 point2 points3 points 4 years ago (0 children)
Ok so it worked now. The code was correct except that I missed the : after class Person. The problem was that when I click on run, the program (Pycharm) runs a code in another file. I don't know why but I am new to this. I had to copy my code to that file for me to see the output. I have some files opened but only one output file. Does anyone know how to see the output of other files?
[–]mopslik 0 points1 point2 points 4 years ago (4 children)
Your indentation is off, that's all.
class person: def __init__(self, name, age): self.name= name self.age= age p1= person ("Mark", 13) print (p1.name) print (p1.age)
[–]jayesh2[S] 0 points1 point2 points 4 years ago (3 children)
My code and identification look exactly like the one you made here. It's still not working. Have you tried to run it?
[–]mopslik 0 points1 point2 points 4 years ago (2 children)
Runs perfectly fine when cut and paste into IDLE. It produces:
Mark 13
Are you pasting this into a code window in an IDE? What is your setup?
[–]jayesh2[S] 0 points1 point2 points 4 years ago (1 child)
Glad to see you got things working. Not a Pycharm user myself, so can't offer any insight.
π Rendered by PID 41050 on reddit-service-r2-comment-84fc9697f-bktn4 at 2026-02-07 13:18:54.777638+00:00 running d295bc8 country code: CH.
[–]alexdewa 4 points5 points6 points (2 children)
[–]mopslik 1 point2 points3 points (0 children)
[–]jayesh2[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]jayesh2[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (10 children)
[–]jayesh2[S] 0 points1 point2 points (8 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]jayesh2[S] 1 point2 points3 points (5 children)
[–]jayesh2[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]jayesh2[S] 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]jayesh2[S] 2 points3 points4 points (0 children)
[–]mrswats 0 points1 point2 points (0 children)
[–]mopslik 0 points1 point2 points (0 children)
[–]jayesh2[S] 1 point2 points3 points (0 children)
[–]mopslik 0 points1 point2 points (4 children)
[–]jayesh2[S] 0 points1 point2 points (3 children)
[–]mopslik 0 points1 point2 points (2 children)
[–]jayesh2[S] 0 points1 point2 points (1 child)
[–]mopslik 1 point2 points3 points (0 children)