0

if i enter add post! nd why does it loops 3times!! Please Explain please!! and also modify my code to make it loop once!! please

class User(): def __init__(self, fname, lname): self.name1 = fname self.name2 = lname self.user = ' ' self.user = fname + ' ' + lname def describe_user(self): print('This is ' + self.user.title() + '!') def greet_user(self): print('Hello, ', self.user.title()) class Admin(User): def __init__(self, fname, lname): super().__init__(fname, lname) def add_privileges(self, user, privileges=[ ]): print('\n') for privilege in privileges: if user == 'add post': print('You', privileges[0]) elif user == 'delete post': print('You', privileges[1]) elif user == 'ban user': print('You', privileges[2]) else: print('Error') my_code = Admin('lucky', 'nayak') my_code.describe_user() my_code.greet_user() my_code.add_privileges('add post', ['can add post', 'can delete post', 'can ban user'])

17th May 2020, 6:23 AM
Lucky Nayak
Lucky Nayak - avatar
3 ответов
17th May 2020, 6:58 AM
ycsvenom
ycsvenom - avatar
+ 1
Thanks to YCS and Slick too!! im sorry i cant give a best tag!! but next time for sure!! bcz i found ycs stuff superb
17th May 2020, 7:32 PM
Lucky Nayak
Lucky Nayak - avatar
0
Because the add_priveleges() function has a for loop in it. "for privelege in priveleges:" That line makes it so if you provide a list as "priveleges" it will loop through the amount of items you have in the list (There are 3 items in the list you added in the last line so it loops 3 times)
17th May 2020, 9:25 AM
Slick
Slick - avatar