1.9-4的代码中,我写的错误代码:

class Restaurant:
    def __init__(self,restaurant_name,cuisine_type) :
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0
        

    def describe_restaurant(self):
        print(f"'restaurant_name:'{self.restaurant_name},'cuisine_type:'{self.cuisine_type}")

    def open_restaurant(self): 
        print('餐馆正在营业')

    def set_number_served(self,number ):
        self.number_served = number

    def increment_number_served(self,morenumber):
        self.number_served += morenumber
   
info1 = Restaurant('Sweet potato salad', 'italian')
info1.describe_restaurant()
info1.open_restaurant()
num1 = info1.set_number_served
print(f"'number_served:'{num1}')" )
num2 = info1.increment_number_served(5)
print(f"'number_served:'{num2}')" )
num3 = info1.increment_number_served(6)
print(f"'number_served:'{num3}')" )

GPT解释:
2023-06-27T14:38:36.png

修正后的代码:

class Restaurant:
    def __init__(self,restaurant_name,cuisine_type) :
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0
        

    def describe_restaurant(self):
        print(f"'restaurant_name:'{self.restaurant_name},'cuisine_type:'{self.cuisine_type}")

    def open_restaurant(self): 
        print('餐馆正在营业')

    def set_number_served(self,number ):
        self.number_served = number

    def increment_number_served(self,morenumber):
        self.number_served += morenumber
    
    
info1 = Restaurant('Sweet potato salad', 'italian')
info1.describe_restaurant()
info1.open_restaurant()
info1.number_served
print(f"'number_served:'{info1.number_served}')" )
info1.set_number_served(5)
print(f"'number_served:'{info1.number_served}')" )
info1.increment_number_served(6)
print(f"'number_served:'{info1.number_served}')" )

2.9-7的代码中,我写的错误代码:

class User:
    def __init__(self,first_name,last_name,sex) :
        self.first_name = first_name
        self.last_name = last_name
        self.sex = sex
        self.login_attempts = 0

    def describe_user(self) :
        print("Name:",self.first_name.capitalize(),self.last_name.capitalize())
        print("Sex:",self.sex)

    def greet_user(self) : 
        print("Hello, I am ",self.first_name.capitalize(),self.last_name.capitalize())   
        # print('---------------------')

    def increment_login_attempts(self) : 
        self.login_attempts += 1
        print('---------------------')
        print("Number of success logins:",self.login_attempts)   
        print('---------------------')
        
    def reset_login_attempts(self) : 
        self.login_attempts = 0
        print('---------------------')
        print("Number of failed logins:",self.login_attempts)   
        print('---------------------')

user1 = User("John", "Doe", "man")
user1.increment_login_attempts()
user1.describe_user()
user1.greet_user()
user1.reset_login_attempts()

# user2 = User("Jane", "Doe", "woman")
# user2.increment_login_attempts()
# user2.describe_user()
# user2.greet_user()
# user2.reset_login_attempts()

class Privileges:
    def __init__(self, privileges):
        self.privileges = ['can add post','can delete post','can ban user']
        
class Admin(User):
    def __init__(self, first_name, last_name, sex):
        super().__init__(first_name, last_name, sex)

    def show_privileges(self) :
        self.privileges = Privileges()
        for p in self.privileges:
            print(self.first_name,self.last_name,'is admin,',p)
        
admin1 =  Admin("Tom", "Jerry", "cat")
admin1.show_privileges()

GPT解释:
2023-06-28T05:02:11.png

修正后的代码:

class User:
    def __init__(self,first_name,last_name,sex) :
        self.first_name = first_name
        self.last_name = last_name
        self.sex = sex
        self.login_attempts = 0

    def describe_user(self) :
        print("Name:",self.first_name.capitalize(),self.last_name.capitalize())
        print("Sex:",self.sex)

    def greet_user(self) : 
        print("Hello, I am ",self.first_name.capitalize(),self.last_name.capitalize())   
        # print('---------------------')

    def increment_login_attempts(self) : 
        self.login_attempts += 1
        print('---------------------')
        print("Number of success logins:",self.login_attempts)   
        print('---------------------')
        
    def reset_login_attempts(self) : 
        self.login_attempts = 0
        print('---------------------')
        print("Number of failed logins:",self.login_attempts)   
        print('---------------------')

user1 = User("John", "Doe", "man")
user1.increment_login_attempts()
user1.describe_user()
user1.greet_user()
user1.reset_login_attempts()

class Privileges:
    def __init__(self, privileges= None):
        if privileges is None:
            self.privileges = []
        else:
            self.privileges = privileges
        
        
class Admin(User):
    def __init__(self, first_name, last_name, sex):
        super().__init__(first_name, last_name, sex)
        self.privileges = Privileges([])

    def show_privileges(self) :
        for p in self.privileges:
            print(self.first_name,self.last_name,'is admin,',p)
        
admin1 =  Admin("Tom", "Jerry", "cat")
admin1.privileges = ['can add post','can delete post','can ban user']
admin1.show_privileges()

3.9-9

在Python中,title是字符串对象的一个方法,用于将字符串中的每个单词的首字母大写。而title()是调用该方法的语法,表示要执行title方法并返回结果。

具体来说,long_name.title表示对title方法的引用,但没有实际调用该方法,所以返回的是方法本身而不是方法的结果。而long_name.title()表示调用title方法,并返回方法的结果,即将long_name中的每个单词的首字母大写后的字符串。