Reputation: 35
class Friend:
def __init__(self,fullname,domicile):
self.fullname = fullname
self.domicile = domicile
def __repr__(self):
return ('Friend {}'.format(self.fullname))
def first_letter(self):
firstletter = self.fullname[0]
print (firstletter)
return firstletter
fnd_1 = Friend('Emilia Asikainen', 'Kintsu')
fnd_2 = Friend('Edi Eskola', 'Muurame')
fnd_3 = Friend('Esa Simonen', 'Jämsä')
fl = ''
fl = fnd_1.first_letter
print(fl)
if fnd_1.first_letter == fnd_2.first_letter and fnd_2.first_letter == fnd_3.first_letter:
print('Do you have a thing with letter {}?'.format(fnd_1.first_letter))
So when I run this code, it doesn't print anything. The only output I get is
Why does my code do this and how can I fix it? I want to get those print methods to work.
Upvotes: 1
Views: 43
Reputation: 75
You simply forgot to invoke your function everywhere.
To invoke function you should put ()
after name of a function.
For example replace fl = fnd_1.first_letter
with fl = fnd_1.first_letter()
And same for fnd_2
and fnd_3
.
Upvotes: 0
Reputation: 6857
The statement if fnd_1.first_letter == fnd_2.first_letter and fnd_2.first_letter == fnd_3.first_letter
is not correct, because you're comparing the functions themselves, rather than the output. For instance, fnd_1.first_letter
is a function; if you want to actually run the function, and get its result, you have to use the following syntax: fnd_1.first_letter()
. Adding the parentheses is necessary in order to actually call a function.
So, if you change your code to correctly call the functions, the relevant part would look like:
fl = fnd_1.first_letter()
print(fl)
if fnd_1.first_letter() == fnd_2.first_letter() and fnd_2.first_letter() == fnd_3.first_letter():
print('Do you have a thing with letter {}?'.format(fnd_1.first_letter()))
And the output is:
E
E
E
E
E
E
E
Do you have a thing with letter E?
Upvotes: 1