0

I am trying to create a program that allows a user to add each answer to a dict. I am also trying to repeat the process until asked to stop. I am trying to create multiple dicts and then encrypt them. Should I create a dict inside a list to create multiple dicts and how can I validate the information before adding it to the dict.

    import base64
    from datetime import datetime
    from cryptography.fernet import Fernet 
    import pickle
    from base64 import b64decode
    
    def username():
        try:
            while True:
                name = [" ", "-"]
                names = input("Enter Name:")
                for n in name:
                    if n in str(names):
                        break
                if str(names):         
                    break 
            
        except TypeError:
            print('wrong')
        return names
        
    def userDOB():
        try:
            dob = input("Enter your date of birth - MM/DD/XXXX:")
            date = datetime.strptime(dob, '%m/%d/%Y')
            if date:
                print(f' {date.month}/{date.day}/{date.year}')
    
        except ValueError:
            while True:
                print("Please Enter Date Of Birth - MM/DD/XXXX")
                dob = input("MM/DD/XXXX:")
                date = datetime.strptime(dob, '%m/%d/%Y')
                if date:
                    break
        return dob
    
    def userstraddress():
        stradd = [" ", " . "]
        straddress = input("Enter The Street Address:")
        for x in stradd:
            if x in straddress:
                return straddress
    
    
    def useremail():
        email = ["@", "."]
        emailaddress = input("Enter Email Address:")
        for n in email:
            if n in emailaddress:
                try:
                    emails = str(emailaddress)
                    if emails:
                        break
                except TypeError:
                    print("Error")
        return emailaddress
    
    x = {'Name': " ", 'Date of Birth': " ", 'Street Address':" ", 'Email Address': " "}
    
    x.update({'Name':username()}) 
    x.update({'Date of Birth': userDOB()})
    x.update({'Street Address': userstraddress()})
    x.update({'Email Address': useremail()}) 
    
    xx = str(x).encode('utf-8')
    base64_dict = base64.b64encode(xx)
    print(base64_dict)
    xx1 = eval(base64.b64decode(base64_dict))
    print(xx)
prnvbn
  • 700
  • 2
  • 7
  • 25

1 Answers1

0

This code doesn't look good. Typically first you get the raw_data. Then you sanitise data and run validation checks on it as well. The validation logic looks doesn't look like it would work properly. Do not put loop in the except block. The except block is to handle exception, like logging or displaying it or doing something else. An infinite loop in exception block is bad code.

Get the data in a single loop. If data isn't entered properly in that loop, let it fail. Save the entered data into a dictionary or list. Run validation as appropriate for each field as per your requirement. If you need a good and easy data model and validation library check out Pydantic. You can also check validation examples provided there. https://pydantic-docs.helpmanual.io/

codefire
  • 393
  • 1
  • 3
  • 13
  • I rewrote my code at https://stackoverflow.com/q/69559860/17028194 can you click the link to see if I used the pydantic validation methods correctly? – Terr McCoy Oct 13 '21 at 17:30