How can i access a variable declared inside a method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can i access a variable declared inside a method?

Here..wen i tried to print fido.sound nothing happens..even if i called the function bark() in the next line..an error appears..can some one tell me whats happening here? https://code.sololearn.com/cVR7rm5idWuV/?ref=app

12th Mar 2020, 6:37 AM
Y AD Ù
Y AD Ù - avatar
4 Answers
+ 5
That's not how it works. This is how it should be: fido.bark() print(fido.sound)
12th Mar 2020, 6:26 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 7
You need to call the bark() method before accessing the attribute and not after. The attribute will only be created after you run it. (A variable linked to an object is called an attribute)
12th Mar 2020, 7:16 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
12th Mar 2020, 6:32 PM
Y AD Ù
Y AD Ù - avatar
0
Aymane Boukrouh [INACTIVE] thats what i did Fido.bark().sound Called the bark then the attribute..
12th Mar 2020, 1:30 PM
Y AD Ù
Y AD Ù - avatar