1

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,

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.

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()
  • Well, you will need to compare to the existing data base whether that new entry is unique. Thus you will first have to read it, compare input to all stored entries and require new one, if it's duplicate. – planetmaker Dec 22 '17 at 18:42

1 Answers1

0

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. 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:

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:

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".

poompt
  • 248
  • 1
  • 8
  • my password also has to possess at least one number, can u help? thanks anyway. – Patrick Jane Dec 22 '17 at 18:46
  • Ah, just noticed that. Based answer on https://stackoverflow.com/questions/3389574/check-if-multiple-strings-exist-in-another-string – poompt Dec 22 '17 at 19:13