鱼C论坛

 找回密码
 立即注册
查看: 427|回复: 1

请教两个程序的区别

[复制链接]
发表于 2018-11-9 22:15:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#coding:gbk
class User:
        def __init__(self,first_name,last_name):
                self.first_name = first_name
                self.last_name = last_name
                self.login_attempts = 0
               
               
        def describe_user(self):
                fullname = self.first_name.title()+' '+self.last_name.title()
                return fullname.title()
               
        def greet_user(self):               
                print('Hi ,are u ok ? ')
       
        def increment_login_attempts(self):
                self.login_attempts += 1
                return self.login_attempts #为什么我需要return

        def reset_login_attempts(self):
                self.login_attempts = 0
                return self.login_attempts#为什么我需要return
i = User('ng','young')
print(i.describe_user())

i.greet_user()

i.increment_login_attempts()
i.increment_login_attempts()
i.increment_login_attempts()
print(i.increment_login_attempts())

print(i.reset_login_attempts())

==============为什么下边的程序不需要return======================================
class User():
    """Represent a simple user profile."""

    def __init__(self, first_name, last_name, username, email, location):
        """Initialize the user."""
        self.first_name = first_name.title()
        self.last_name = last_name.title()
        self.username = username
        self.email = email
        self.location = location.title()
        self.login_attempts = 0

    def describe_user(self):
        """Display a summary of the user's information."""
        print("\n" + self.first_name + " " + self.last_name)
        print("  Username: " + self.username)
        print("  Email: " + self.email)
        print("  Location: " + self.location)

    def greet_user(self):
        """Display a personalized greeting to the user."""
        print("\nWelcome back, " + self.username + "!")

    def increment_login_attempts(self):
        """Increment the value of login_attempts."""
        self.login_attempts += 1

    def reset_login_attempts(self):
        """Reset login_attempts to 0."""
        self.login_attempts = 0

eric = User('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska')
eric.describe_user()
eric.greet_user()

print("\nMaking 3 login attempts...")
eric.increment_login_attempts()
eric.increment_login_attempts()
eric.increment_login_attempts()
print("  Login attempts: " + str(eric.login_attempts))

print("Resetting login attempts...")
eric.reset_login_attempts()
print("  Login attempts: " + str(eric.login_attempts))

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-11-9 22:18:07 | 显示全部楼层
看懂了,请删帖
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 09:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表