帮我优化以下代码user_list = [ {'user': 'zhangsan', 'password': '123456', 'balance': 1000}, {'user': 'lisi', 'password': '111111', 'balance': 2500}, {'user': 'wangwu', 'password': '252525', 'balance': 100} ] current_user = None # 用于记录当前登陆用户信息的全局变量 def reg(): while True: un = input('请输入您的用户名:【注册】') for item in user_list: if un == item['user']: print('用户已存在,请检查') break # break 出for循环 else: pw = input('请输入您的密码:【注册】') if len(pw) < 6: print('密码长度小于6位,请检查') else: # 用户注册成功 user_list.append({'user': un, 'password': pw, 'balance': 3000}) print('恭喜您注册成功!') return True # 退出整个函数 def login(): while True: un = input('请输入您的用户名:【登陆】') pw = input('请输入您的密码:【登陆】') for user in user_list: if user['user'] == un and user['password'] == pw: # 这就说明用户名和密码输入正确 print('恭喜您登陆成功!') global current_user current_user = user return else: print('用户名或密码错误!') def check_balance(): if current_user: # 代表当前已经登陆 print('当前用户的余额为:', current_user['balance']) else: print('请先登陆后再进行查询余额操作!') def depoisit(): if current_user: money = int(input('请输入您要存款的金额:')) if money[-2:] == '00' and len(money) > 2: # 说明输入的是100的整数倍 current_user['balance'] += int(money) print('恭喜存款成功,当前余额为:', current_user['balance']) else: print('您的存款金额格式不正确,请检查后再操作') else: print('您尚未登陆,请登陆后再进行相关操作!') def withdraw(): if current_user: money = input('请输入您要取款的金额:') if money[-2:] == '00' and len(money) > 2: # 说明输入的是100的整数倍 if current_user['balance'] >= int(money): current_user['balance'] -= int(money) print('恭喜您取款成功,当前余额为:', current_user['balance']) else: print('您的余额不足!') else: print('您的存款金额格式不正确,请检查后再操作') else: print('您尚未登陆,请登陆后再进行相关操作!') def get_menu(): menu = ''' ******欢迎来到WoniuATM******* *********请选择操作菜单********* *****1. 注册 2. 登录 3. 查询余额 4. 存款 5. 取款 6.计算复利 7.取卡 *** ''' while True: print(menu) option = input('请输入您要操作的菜单:') if option == '1': reg() elif option == '2': login() elif option == '3': check_balance() elif option == '4': depoisit() elif option == '5': withdraw() elif option == '6': withdraw() elif option == '7': print('感谢您的使用,欢迎下次再来!') break else: print('选择菜单项错误,请重新选择!') get_menu()
时间: 2023-02-13 18:03:56 浏览: 161
当前的代码定义了一个名为`user_list`的列表,该列表中包含了三个字典,每个字典都包含了用户名,密码和余额信息。
此外,还定义了一个名为`current_user`的变量,但是没有为其赋值。
下面是一些建议,帮助您优化这段代码:
1. 对于字典中的每个键,都应该使用双引号将其包围起来,这样可以避免出现语法错误。例如,可以将字典中的键修改为`"user"`,`"password"`和`"balance"`。
2. 建议将用户信息存储在单独的用户类中,并将用户对象存储在列表中。这样,就可以使用类的方法来操作用户信息,而无需直接访问字典的键。
3. 建议为`current_user`变量赋初始值。例如,可以将其初始化为`None`,表示当前没有用户登录。
例如,可以使用以下代码来定义用户类和用户列表:
```
class User:
def __init__(self, username, password, balance):
self.username = username
self.password = password
self.balance = balance
user_list = [
User("zhangsan", "123456", 1000),
User("lisi", "111111", 2500),
User("wangwu", "252525", 100)
]
current_user = None
```
相关问题
class Student: __School="湖州师范学院" def __init__(self,sname,sno,ssex,sage): self.__sname=sname self.__sno=sno self.__ssex=ssex self.__sage=sage print(Student.__School) s=Student("zhangsan","2022082201","F",19) print(s.__sname,s.__sno,s.__ssex,s.__sage,s.__School)
这是一个定义了学生类的代码,并创建了一个学生对象s。在这个代码中,类属性__School是私有的,实例属性__sname、__sno、__ssex和__sage也是私有的。以下是代码的输出结果:
湖州师范学院
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
AttributeError: 'Student' object has no attribute '__sname'
在第一行中,输出了类属性__School的值。在第二行中,试图输出s对象的属性值,但会出现错误。因为属性__sname是私有的,不能直接访问。如果要访问,需要通过类提供的公有方法或属性进行访问。
(3)(基础题)阅读以下的程序,回答程序后的问题: class people(object): __te="隐私" def __init__(self,name,age,weight): self.name=name self.age=age self.weight=weight def eat(self,x): self.weight=self.weight+x*0.01 def __fun(self): pass def _fun2(self): pass def getyinsi(self): return self.__te class student(people): def __init__(self,s,m): self.no=s self.score=m def getscore(self): print("%s的成绩为%s"%(self.name,self.score)) zhangsan=people("张三",20,100) zhangsan.__________(1)__________________ lisi=student("100000001",95) lisi.________________(2)_________________ 回答以下问题: ①父类people拥有几个成员?分别列出来。 一个成员,张三 属性: 方法:__init__()、eat()、__fun()、 ②子类student拥有几个成员?分别列出来。 属性: 方法: ③在后面zhangsan对象的引用位置(1)可以填写的成员有: ④在后面lisi对象的引用位置(2)可以填写的成员有:
①父类people拥有三个成员:属性__te,方法__init__()和方法eat()。
②子类student拥有两个成员:属性no和属性score。
③在后面zhangsan对象的引用位置(1)可以填写的成员有:eat(x)。
④在后面lisi对象的引用位置(2)可以填写的成员有:getyinsi()和getscore()。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.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)