def zq(sender, receiver, amount, balance): if amount < balance: print(f"{sender}向{receiver}转账{amount}元成功") return True else: print(f"{sender}向{receiver}转账{amount}元失败") return False sender='12345678900' receiver='55566679800' amount=input("请输入转账金额:") balance=10000请帮我改进代码
时间: 2024-03-22 08:41:42 浏览: 94
首先,我们需要将输入的金额转换为数字类型。其次,我们可以将余额默认设置为函数的参数,而不是每次都要输入。最后,我们可以将函数的返回值改为返回转账后的余额,以便我们可以在后续的代码中使用。
下面是改进后的代码:
```
def zq(sender, receiver, amount, balance=10000):
amount = float(amount)
if amount <= balance:
print(f"{sender}向{receiver}转账{amount}元成功")
balance -= amount
return balance
else:
print(f"{sender}向{receiver}转账{amount}元失败")
return balance
sender = '12345678900'
receiver = '55566679800'
amount = input("请输入转账金额:")
balance = zq(sender, receiver, amount)
print(f"转账后余额为: {balance}")
```
这样,我们就可以在转账后打印出余额,让用户知道他们还剩多少钱。
相关问题
def zzhk(sender, receiver, amount): sender = input("请输入转账人手机号:") receiver = input("请输入被转账人手机号:") amount = input("请输入转账金额:") if amount>money: print("余额不足,无法转账") else: return result = zzhk(sender, receiver, amount) if result: print(f'{sender}向{receiver}转账{amount}元成功') else: print(f'{sender}向{receiver}转账{amount}元失败') return True请帮我改善一下代码
以下是改善后的代码:
```python
def zzhk(sender, receiver, amount, balance):
if amount > balance:
print("余额不足,无法转账")
return False
else:
balance -= amount
print(f"{sender}向{receiver}转账{amount}元成功")
print(f"当前余额为{balance}元")
return True
sender = input("请输入转账人手机号:")
receiver = input("请输入被转账人手机号:")
amount = float(input("请输入转账金额:"))
balance = 1000.00 # 假设账户余额为1000元
result = zzhk(sender, receiver, amount, balance)
if not result:
print(f"{sender}向{receiver}转账{amount}元失败")
```
改善的内容如下:
1. 增加了一个 `balance` 参数,表示账户余额,可以避免每次调用函数时都要输入余额的麻烦。
2. 将判断余额是否足够的逻辑放到了函数中,避免了重复代码。
3. 将成功或失败的提示信息放到了函数外部,使函数只负责转账操作,提高了代码的复用性和可维护性。
4. 将 `amount` 转换成了浮点数类型,避免了字符串和数值之间的比较错误。
def dl(): while True: id = '1111' passwd = '1111' lock_usr = [] for i in range(0, 3): usr_id = input("学号为:") usr_passwd = input("密码为:") if usr_id == id and usr_passwd == passwd: print("登录成功") show() elif id != usr_id and passwd != usr_passwd: if i < 2: print("学号或者密码错误,请重新输入!") else: lock_usr.append(usr_id) print("对不起!您的帐号被冻结") def show(): print("{:-^40}".format("主菜单")) print("账户查询\t [输入1]") print("存款\t [输入2]") print("取款\t [输入3]") print("转账汇款\t [输入4]") print("投资理财\t [输入5]") print("请输入您的选择:") a = input() if a == "1": cq() elif a == "2": ck() elif a == "3": qk() elif a == "4": zq() elif a == "5": tzlc() else: tc() dl() money = 10000 def cq(): global money name = input("请输入姓名:") print("{:-^40}".format("查询余额")) print(f'{name},您好,您的余额是:{money}') pass def ck(): global money print("{:-^40}".format("存款")) add = eval(input("请输入存款:")) money += add print(f'您好,您存款{add}元成功!') print(f'您好,您的余额是:{money}') pass def qk(): global money print("{:-^40}".format("取款")) less = eval(input("请输入取款:")) money -= less print(f'您好,您取款{less}元成功!') print(f'您好,您的余额是:{money}') pass def zq(sender, receiver, amount, balance): if amount > balance: print("余额不足,无法转账") return False else: balance -= amount print(f'{sender}向{receiver}转账{amount}元成功') print(f'当前余额为{balance}元') return True sender = input("请输入转账人手机号:") receiver = input("请输入被转账人手机号:") amount = float(input("请输入转账金额:")) balance = 10000.00 result = zq(sender, receiver, amount, balance) if not result: print(f'{sender}向{receiver}转账{amount}元失败') pass
这段代码是一个简单的银行账户管理系统,实现了登录、查询余额、存款、取款、转账汇款等功能。用户首先需要输入学号和密码进行登录,登录成功后可以选择不同的操作,比如查询余额、存款、取款、转账汇款等。其中转账汇款需要输入转账人手机号、被转账人手机号和转账金额,系统会判断余额是否足够进行转账,如果余额不足则转账失败。
阅读全文