前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >存折类定义(类与对象)Python

存折类定义(类与对象)Python

作者头像
叶茂林
发布2023-07-30 10:37:46
1840
发布2023-07-30 10:37:46
举报
文章被收录于专栏:叶子的开发者社区

题目描述

定义一个存折类CAccount,存折类具有帐号(account, long)、姓名(name,char[10])、余额(balance,float)等数据成员,可以实现存款(deposit,操作成功提示“saving ok!”)、取款(withdraw,操作成功提示“withdraw ok!”)和查询余额(check)的操作,取款金额必须在余额范围内,否则提示“sorry! over limit!”。

编写主函数,建立这个类的对象并测试,输入账号、姓名、余额后,按照查询余额、存款、查询余额、取款、查询余额的顺序调用类方法并输出。

输入

第一个存折的账号、姓名、余额

存款金额

取款金额

第二个存折的账号、姓名、余额

存款金额

取款金额

输出

第一个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

第二个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

输入样例1 

9111 Tom 1000 500 1000 92220 John 500 500 1500

输出样例1

Tom's balance is 1000 saving ok! Tom's balance is 1500 withdraw ok! Tom's balance is 500 John's balance is 500 saving ok! John's balance is 1000 sorry! over limit! John's balance is 1000

思路分析

呃 有空再写了,先偷个懒。

好吧还是写吧。

一开始写成这样:

代码语言:javascript
复制
class CAccount:
    def datain(self):
        self.account,self.name,self.balance=input(),input(),float(input())

发现这样的话,accout和name会读取整一个带空格的字符串。

于是只能先全读了,再强制转换:

代码语言:javascript
复制
class CAccount:
    def datain(self):
        self.account,self.name,self.balance=input().split()
        self.balance=float(self.balance)

AC代码

代码语言:javascript
复制
class CAccount:
    def datain(self):
        self.account,self.name,self.balance=input().split()
        self.balance=float(self.balance)
    def deposit(self):
        self.plus=int(input())
        self.balance=self.balance+self.plus
        print("saving ok!")
    def withdraw(self):
        self.minus=int(input())
        if self.minus>self.balance:
            print("sorry! over limit!")
            return
        self.balance=self.balance-self.minus
        print("withdraw ok!")
    def check(self):
        print("%s's balance is %.0f"%(self.name,self.balance))
a,b=CAccount(),CAccount()
a.datain()
a.check()
a.deposit()
a.check()
a.withdraw()
a.check()
b.datain()
b.check()
b.deposit()
b.check()
b.withdraw()
b.check()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 思路分析
  • AC代码
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档