Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Print Of A None When Itorating A List Of Objects In Python

I'm trying to Iterate over a list of objects, but I keep having a "None" printed after each line

class UserInfo():
    def __init__(self, name = str(), key = str(), ID = str()):
        self.name = name
        self.key = key
        self.ID = ID
    def info(self):
        print ("the user name is {}, the ID is {} and the key is {}".format(self.name, self.ID, self.key))

user1 = UserInfo( name = 'Mike Mourise',key = '%#^*^%#', ID = 'SVAR231G')
user2 = UserInfo( name = 'Alex Kurt',key = '%#^*^!@', ID = 'SW1I2X45')
user3 = UserInfo( name = 'Kevin Heart',key = '%@(*^%$', ID = 'BOET34617') 
users = [user1, user2, user3]

for person in users:
    print (person.info())

I'm want it to print this:

the user name is Mike Mourise, the ID is SVAR231G and the key is %#^*^%#
the user name is Alex Kurt, the ID is SW1I2X45 and the key is %#^*^!@
the user name is Kevin Heart, the ID is BOET34617 and the key is %@(*^%$

but it is printing this isntead:

the user name is Mike Mourise, the ID is SVAR231G and the key is %#^*^%#
None
the user name is Alex Kurt, the ID is SW1I2X45 and the key is %#^*^!@
None
the user name is Kevin Heart, the ID is BOET34617 and the key is %@(*^%$
None

please help

like image 839
M. Saeb Avatar asked Nov 23 '25 08:11

M. Saeb


1 Answers

# your code goes here
class UserInfo():
    def __init__(self, name , key, ID ):
        self.name = name
        self.key = key
        self.ID = ID
    def info(self):
        #use return
        return ("the user name is {}, the ID is {} and the key is {}".format(self.name, self.ID, self.key))

user1 = UserInfo( name = 'Mike Mourise',key = '%#^*^%#', ID = 'SVAR231G')
user2 = UserInfo( name = 'Alex Kurt',key = '%#^*^!@', ID = 'SW1I2X45')
user3 = UserInfo( name = 'Kevin Heart',key = '%@(*^%$', ID = 'BOET34617') 
users = [user1, user2, user3]

for person in users:
    print (person.info())

The reason why it printed None was because you never returned anything. Instead of using print, use return for the function.

like image 176
jukebox Avatar answered Nov 25 '25 22:11

jukebox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!