Thanks to visit codestin.com
Credit goes to github.com

Skip to content

New to python had a problem #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Satya-SriRam-Varma opened this issue Jan 31, 2019 · 10 comments
Open

New to python had a problem #476

Satya-SriRam-Varma opened this issue Jan 31, 2019 · 10 comments

Comments

@Satya-SriRam-Varma
Copy link

Satya-SriRam-Varma commented Jan 31, 2019

hello,everyone I was having time to learn python so i started learning it from day before yesterday,now I am trying to make snake game from curses module and I ran into A TROUBLE, figured someone from here would lend me some of their experience regarding this
the document is the photograph of the code fragment
Document 8.pdf

class Snake(object):
def init(self):
self.x='Hiss!'
def method_a(self,foo):
print(self.x +' '+ foo)

snake = Snake()
snake.method_a('Says the snake')

the above code fragment is having an error
upon trying to run it the terminal gives the following

Traceback (most recent call last):
File "hc.py", line 31, in
snake.method_a('Says the snake')
File "hc.py", line 28, in method_a
print(self.x +' '+ foo)
AttributeError: 'Snake' object has no attribute 'x'

can't find where the error is so i request you guys to please help me if you know any thing regarding this

and sorry i forgot the most important part of info iam using python 3.7.2 64 bit on windows 7

@AronYang
Copy link

replace 'init' with __init__

@AronYang
Copy link

I see your picture. that is _init_. the correct is __init__

@Satya-SriRam-Varma
Copy link
Author

Thank you AronYang

@HoreeChen
Copy link

good eye!

@Abhi050
Copy link

Abhi050 commented Feb 13, 2019

"""This is my first help in the open source or first comment """
Replace init by( init )
the two underscores before and after init

@Andrew-LC
Copy link

Hey your code:
class Snake(object):
def init(self):
self.x='Hiss!'
def method_a(self,foo):
print(self.x +' '+ foo)

snake = Snake()
snake.method_a('Says the snake')

Mistakes:
#1 init --> <init>
#2 init ---> it initializes code variable , so any code inside it is global i.e restricited to itself and not
availabe to method_a
#3 var x has not been made in the instance i.e ---> [def__init__(self, x):]--> corrrect code
#4 if you make x global within init, you will get this message: <SyntaxError: name 'x' is local and global>

My code:
class Snake():
global x
x='Hiss!'
def method_a(self,foo):
print(x +' '+ foo)

snake = Snake()
snake.method_a('Says the snake')

I hope the answer helps you.

@Unknow-gui
Copy link

give you a thubs up

@lkkplau0988
Copy link

I copy it to run on python first,I don't know the error

@MarvinKweyu
Copy link

Can you be more specific @ikkplau0988?

@lkkplau0988
Copy link

I try to run the program again,thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants