第四十四天Python学习记录

Posted xudachen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第四十四天Python学习记录相关的知识,希望对你有一定的参考价值。

需求:

修改个人信息程序

在一个文件里存多个人的个人信息,如以下

username password  age position department 
alex     abc123    24   Engineer   IT
rain     [email protected]    25   Teacher   Teching
....

1.输入用户名密码,正确后登录系统 ,打印

1. 修改个人信息
2. 打印个人信息
3. 修改密码

2.每个选项写一个方法

3.登录时输错3次退出程序

  1 def print_personal_info(account_dic, username):
  2     """
  3     print user info
  4     :param account_dic: all account‘s data
  5     :param username: username
  6     :return: None
  7     """
  8     person_data = account_dic[username]
  9     info = ‘‘‘
 10     ------------------
 11     Name:   %s
 12     Age :   %s
 13     Job :   %s
 14     Dept:   %s
 15     Phone:  %s
 16     ------------------
 17     ‘‘‘ % (person_data[2],
 18            person_data[3],
 19            person_data[4],
 20            person_data[5],
 21            person_data[6],
 22            )
 23 
 24     print(info)
 25 
 26 
 27 def save_back_to_file(account_dic):
 28     """
 29     把account_dic转换成字符串格式,写回文件
 30     :param account_dic:
 31     :return:
 32     """
 33     f.seek(0)  # 回到文件头
 34     f.truncate()  # 清空原文件
 35 
 36     for k in account_dic:
 37         row = ",".join(account_dic[k])
 38         f.write("%s\n" % row)
 39 
 40     f.flush()
 41 
 42 
 43 def change_personal_info(account_dic, username):
 44     """
 45     change user info , 思路如下
 46     1.把这个人的每个信息打印出来,让其选择修改哪个字段,用户选择了数字,正好是字段的索引,这样直接把字段找出来修改掉就可以了
 47     2.改完后,还要把这个新数据重新写回到account.txt,由于改完后的新数据是字典类型,还需要把字典转换成字符串后,再写回硬盘
 48     :param account_dic:
 49     :param username:
 50     :return:
 51     """
 52     person_data = account_dic[username]
 53     print("person data:", person_data)
 54     column_names = [Username, Password, Name, Age, Job, Dept, Phone]
 55     for index, k in enumerate(person_data):
 56         if index > 1:  # 0 is username and 1 is password
 57             print("%s.  %s: %s" % (index, column_names[index], k))
 58 
 59     choice = input("select column id to change:").strip()
 60     if choice.isdigit():
 61         choice = int(choice)
 62         if 0 < choice < len(person_data):
 63             column_data = person_data[choice]
 64             print("current value>:", column_data)
 65             new_val = input("new value>:").strip()
 66             if new_val:
 67                 person_data[choice] = new_val
 68                 print(person_data)
 69 
 70                 save_back_to_file(account_dic)
 71             else:
 72                 print("输入为空")
 73 
 74 
 75 def change_user_password(account_dic, username):
 76     """
 77     change user info , 思路如下
 78     1.把这个人的密码打印出来,让用户修改
 79     2.改完后,还要把这个新数据重新写回到account.txt,
 80     :param account_dic:
 81     :param username:
 82     :return:
 83     """
 84     person_data = account_dic[username]
 85     print("person data:", person_data)
 86     column_names = [Username, Password, Name, Age, Job, Dept, Phone]
 87     for index, k in enumerate(person_data):
 88         if index < 2:  # 0 is username and 1 is password
 89             print("%s.  %s: %s" % (index, column_names[index], k))
 90 
 91     choice = input("select column id to change:").strip()
 92     if choice.isdigit():
 93         choice = int(choice)
 94         if 0 < choice < len(person_data):
 95             column_data = person_data[choice]
 96             print("current value>:", column_data)
 97             new_val = input("new value>:").strip()
 98             if new_val:
 99                 person_data[choice] = new_val
100                 print(person_data)
101 
102                 save_back_to_file(account_dic)
103             else:
104                 print("输入为空")
105 
106 
107 account_file = "account.txt"
108 f = open(account_file, "r+", encoding="utf-8")
109 raw_data = f.readlines()
110 accounts = {}
111 
112 for line in raw_data:
113     line = line.strip()
114     if not line.startswith("#"):
115         items = line.split(",")
116         accounts[items[0]] = items
117 
118 menu = ‘‘‘
119 1.打印个人信息
120 2.修改个人信息
121 3.修改密码
122 ‘‘‘
123 
124 count = 0
125 while count < 3:
126     username = input("Username:").strip()
127     password = input("Password:").strip()
128     if username in accounts:
129         if password == accounts[username][1]:
130             print("welcome %s ".center(50, -) % username)
131             while True:
132                 print(menu)
133                 user_choice = input(">>>").strip()
134                 if user_choice.isdigit():
135                     user_choice = int(user_choice)
136                     if user_choice == 1:
137                         print_personal_info(accounts, username)
138                     elif user_choice == 2:
139                         change_personal_info(accounts, username)
140                     elif user_choice == 3:
141                         change_user_password(accounts, username)
142 
143                 elif user_choice == q:
144                     exit("bye.")
145         else:
146             print("wrong username or password")
147     else:
148         print("Username does not exist.")
149     count += 1
150 
151 else:
152     print("Too many attempts.")

 

以上是关于第四十四天Python学习记录的主要内容,如果未能解决你的问题,请参考以下文章

第四百一十四节,python常用算法学习

第四天Python学习记录

“全栈2019”Java第四十四章:继承

WPF学习第四十四章 图画

猛新自闭学习第四天 第四章

python小白学习记录 多线程爬取ts片段