简体   繁体   English

新手python-在txt文件中添加“ cilent”

[英]newbie python - adding 'cilent' into the txt file

I'm working on a registration in my school project, and one part of it is about registartion. 我正在学校项目中进行注册,其中一部分与注册有关。 I have to input client in form of 我必须以以下形式输入客户

  1. username 用户名
  2. password 密码
  3. name 名称
  4. lastname
  5. role 角色

so he can be registered and appended into the txt file, 这样他就可以注册并附加到txt文件中,

but i also have to make "username" be the unique in file (because I will have the other cilents too) and "password" to be longer than 6 characters and to possess at least one number. 但我还必须使“用户名”成为文件中的唯一名称(因为我也将拥有其他词),“密码”必须长于6个字符并拥有至少一个数字。

Btw role means that he is a buyer. 顺便说一句,这意味着他是买家。 The bolded part I didn't do and I need a help if possible. 我没有做的粗体部分,如果可能,我需要帮助。 thanks 谢谢

def reg()
  f = open("svi.txt","a")

  username = input("Username: ")
  password = input("Password: ")
  name = input("Name of a client: ")
  lastname = input("Lastname of a client: ")
  print()
  print("Successful registration! ")
  print()

  cilent = username + "|" + password + "|" + name + "|" + lastname + "|" + "buyer"

  print(cilent,file = f)
  f.close()

You need to add some file parsing and logic in order to accomplish this. 为此,您需要添加一些文件解析和逻辑。 Your jobs are to: 您的工作是:

1: Search the existing file to see if the username exists already. 1:搜索现有文件以查看用户名是否已存在。 With the formatting as you've given it, you need to search each line up to the first '|' 使用给定的格式,您需要搜索每行直到第一个“ |” and see if the new user is uniquely named: 并查看新用户是否具有唯一名称:

name_is_unique = True
for line in f:
   line_pieces = line.split("|")
   test_username = line_pieces[0]
   if test_username == username:
      name_is_unique = False
      print("Username already exists")
      break

2: See if password meets criteria: 2:查看密码是否符合条件:

numbers=["0","1","2","3","4","5","6","7","8","9"]
password_contains_number = any(x in password for x in numbers)
password_is_long_enough = len(password) > 6

3: Write the new line only if the username is unique AND the password meets your criteria: 3:仅当用户名唯一且密码符合您的条件时,才写新行:

if name_is_unique and password_contains_number and password_is_long_enough:
   print(cilent,file = f)

Edit: You may also have to open it in reading and writing mode, something like "a+" instead of "a". 编辑:您可能还必须以读写模式打开它,例如“ a +”而不是“ a”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM