Python基本语法实训练习开发购物系统、邮箱账号和密码的检测;
时间: 2024-02-06 16:03:44 浏览: 55
好的,我可以给你提供一个简单的 Python 实训练习,实现一个购物系统并检测邮箱账号和密码。
首先,我们需要定义一些变量来存储商品信息和用户信息。我们可以使用一个字典来存储商品信息,其中键是商品名称,值是商品价格。我们还需要一个列表来存储用户信息,其中每个元素都是一个字典,包含用户的邮箱账号和密码。
```
# 商品信息
products = {
'apple': 5,
'banana': 2,
'orange': 3,
'grape': 4,
'watermelon': 10
}
# 用户信息
users = [
{'email': 'test1@example.com', 'password': '123456'},
{'email': 'test2@example.com', 'password': 'abcdef'}
]
```
接下来,我们需要编写一个函数来检测用户输入的邮箱账号和密码是否正确。我们可以遍历用户列表,比较输入的邮箱账号和密码是否与列表中的某个元素匹配。
```
def check_login(email, password):
for user in users:
if user['email'] == email and user['password'] == password:
return True
return False
```
现在我们可以编写一个主函数来实现购物系统。首先,我们需要提示用户输入邮箱账号和密码,并检测其是否正确。如果账号和密码正确,我们可以进入购物系统。否则,我们应该提示用户重新输入。
```
def main():
while True:
email = input('Please enter your email: ')
password = input('Please enter your password: ')
if check_login(email, password):
print('Login successful!')
break
else:
print('Email or password is incorrect. Please try again.')
# 进入购物系统
total_price = 0
while True:
product_name = input('Please enter the product name: ')
if product_name == 'exit':
break
elif product_name not in products:
print('Product not found.')
continue
product_price = products[product_name]
total_price += product_price
print('Added {} to your cart. Total price: {}.'.format(product_name, total_price))
print('Thank you for shopping! Your total price is {}.'.format(total_price))
if __name__ == '__main__':
main()
```
现在我们已经完成了 Python 实训练习,实现了一个简单的购物系统并检测了邮箱账号和密码。需要注意的是,这个实现方式仅供参考,实际开发中需要考虑更多的安全性和健壮性问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)