python免费代码大全

ATM机的Python代码

ATM(Automated Teller Machine,自动柜员机),它是银行业推行的经济管道,在电子化快速发展的今天,ATM机几乎成为银行中不可缺少的工具之一,它带来了方便、快捷、周到的金融体验。

ATM机的基本功能包括取钱、存钱、查询账户余额、转账等。下面是一个简单的ATM机Python代码实现。

代码实现

我们假设有三个用户,如下所示:

```

users = {

"Tom": {"password": "password1", "balance": 500},

"Jack": {"password": "password2", "balance": 1000},

"Jane": {"password": "password3", "balance": 1500}

}

```

目前该ATM机可实现以下功能:

1. 账户验证:输入用户名和密码,进行账户验证。

2. 查询余额:输入用户名和密码后,可以查询账户余额。

3. 存钱:输入用户名和密码后,可以选择存款并输入金额进行存款。

4. 取钱:输入用户名和密码后,可以选择取款并输入金额进行取款。

5. 转账:输入转出账户、转入账户、转账金额后,进行转账。

代码如下:

```

users = {

"Tom": {"password": "password1", "balance": 500},

"Jack": {"password": "password2", "balance": 1000},

"Jane": {"password": "password3", "balance": 1500}

}

def account_validate(name, password):

if name in users and users[name]["password"] == password:

return True

else:

return False

def get_balance(name, password):

if account_validate(name, password):

balance = users[name]["balance"]

print(f"Your balance is: {balance}")

else:

print("Invalid account or password.")

def deposit(name, password, amount):

if account_validate(name, password):

users[name]["balance"] += amount

balance = users[name]["balance"]

print(f"You deposited {amount}, your balance is now {balance}")

else:

print("Invalid account or password.")

def withdraw(name, password, amount):

if account_validate(name, password):

if users[name]["balance"] >= amount:

users[name]["balance"] -= amount

balance = users[name]["balance"]

print(f"You withdrew {amount}, your balance is now {balance}")

else:

print("Insufficient balance.")

else:

print("Invalid account or password.")

def transfer(sender_name, sender_password, receiver_name, amount):

if account_validate(sender_name, sender_password):

if users[sender_name]["balance"] >= amount:

users[sender_name]["balance"] -= amount

users[receiver_name]["balance"] += amount

sender_balance = users[sender_name]["balance"]

receiver_balance = users[receiver_name]["balance"]

print(f"You transferred {amount} from {sender_name} to {receiver_name}.")

print(f"{sender_name}'s balance is now {sender_balance}, {receiver_name}'s balance is now {receiver_balance}.")

else:

print("Insufficient balance.")

else:

print("Invalid account or password.")

```

使用示例:

```

>>> get_balance("Tom", "password1")

Your balance is: 500

>>> deposit("Jane", "password3", 500)

You deposited 500, your balance is now 2000

>>> withdraw("Jack", "password2", 5000)

Insufficient balance.

>>> withdraw("Jack", "password2", 500)

You withdrew 500, your balance is now 500

>>> transfer("Jane", "password3", "Jack", 500)

You transferred 500 from Jane to Jack.

Jane's balance is now 1500, Jack's balance is now 1000.

```

相关知识

在实现ATM机Python代码的过程中,有一些知识点需要了解。

字典:Python中的字典是一种映射类型,表示键(key)和值(value)之间的映射关系。具有高效查询、插入、删除等特点,是Python中比较常用的数据类型之一。上面代码中,我们使用字典存储了用户信息。

函数:Python中的函数是一段可重用的代码块,用于完成特定的任务。在ATM机Python代码中,我们使用函数实现了账户验证、查询余额、存钱、取钱和转账等功能。

格式化字符串:Python中格式化字符串是一种字符串替换方法,使用一对花括号{}表示占位符,在输出时将占位符替换为变量的值。在ATM机Python代码中,我们使用格式化字符串输出余额、交易信息等。

总结

ATM机Python代码实现并不复杂,但也涉及到了一些基本的Python语法和本地存储方式。在实际开发中,还需要实现更多的功能,如密码加密、交易记录更新、转账审核等,以提高安全性和可靠性。 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.ynyuzhu.com/

点赞(58) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部