Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
incomplete registration
#1
I know you guys have seen me post questions on registration programs... This is, I have not completed one that I actually like. It will be the start of creating a website / video game / anything I decide to program. Registration and login is important riiiiiiight.... so I just wanted to say I think because of the help from some of the classmates in CS50 and the folks on this forum I have come a long way in just a few months. I have not been programming very long at all... maybe a few months. I know this code is not as efficient as it could be but here we have the start of a registration form that I may stick with because I believe it's coming along pretty well for a newbie. If anyone has any suggestions I'd love the constructive crit. or of course if you just want something to do I will post on git if you request. I still need to add conditions to username and password to make sure special characters aren't used and length is appropriate, etc, still a lot of work to do buuuuuuuuuutttttt here goes nothing:

#!/usr/bin/env python3
import json
import time
import sys

def get_username():
    prompt_username = 'What is the username you wish to create?'
    username = input(prompt_username + '\n')
    return username
    
def get_password():
    prompt_password = 'Create a password'
    password = input(prompt_password + '\n')
    return password
    
    
def create_newUserList(username, password):
    userList = []
    userList.append(username)
    userList.append(password)
    return userList
    
def make_newUserDict(userList):
    convert = userList
    userDict = {}
    userDict[convert[0]] = convert[1]
    return userDict

def get_userDataDict():    
    filename = 'userdata.json'
    try:
        with open(filename) as f_obj:
            userdata = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return userdata
        
def create_userdataFile(userDict):
    filename = 'userdata.json'
    with open(filename, 'w') as f_obj:
        json.dump(userDict, f_obj)

def check_new_old(username, existing_usernames):
    new_username = username
    old_usernames = existing_usernames
    
    for k in old_usernames.keys():
        if new_username[0].lower() == k.lower():
            print('Username taken, prompting for new entry.')
            return None
        else:
            print('Username available.')
            return new_username

def add_goodUserInfo(good_userInfo, existing_userData):
    data_to_add = good_userInfo
    existing_data = existing_userData
    existing_data[data_to_add[0]] = data_to_add[1]
    
    filename = 'userdata.json'
    with open(filename, 'w') as f_obj:
        json.dump(existing_data, f_obj)

def main():
    
    while True:
        username = get_username()
        password = get_password()
        userList = create_newUserList(username, password)
        userDict = make_newUserDict(userList)
        existing_userData = get_userDataDict()
        
        if existing_userData:
            good_userInfo = check_new_old(userList, existing_userData)
            if good_userInfo:
                print('Adding username ' + good_userInfo[0] +
                ' to our system.')
                time.sleep(5)
                add_goodUserInfo(good_userInfo, existing_userData)
                print('Welcome, ' + good_userInfo[0] + '!')
                break
            else:
                print('Reprompt')
                continue
        else:
            create_userdataFile(userDict)
            break
    
    
main()
This isn't really asking for help unless you'd just like to help out... But more just claiming a happy coders feelings through accomplishment. ;-) Lets chat on it a bit Mr. Experienced, I'd love to learn more! Feed me knowledge! In advance I thank you!
Reply
#2
Fixed some repetitive stuff... IE:

def check_new_old(username, existing_usernames):
    new_username = username
    old_usernames = existing_usernames
     
    for k in old_usernames.keys():
        if new_username[0].lower() == k.lower():
            print('Username taken, prompting for new entry.')
            return None
        else:
            print('Username available.')
            return new_username
to:

def check_new_old(username, existing_usernames):
    invalid_chars = '~ ` ! @ # $ % ^ & * ( ) + = { [ } ] : ; " \' < , > . ? / | \ '.split()
    if any(x in invalid_chars for x in list(username[0])) or (' ' in list(username[0])) or (len(username[0]) > 12):
        print('Username can\'t contain invalid characters and no longer than 12 characters: \n'
        + str(invalid_chars))
        return None
    else:
        print('Need to fix check')
        pass
    
    for k in existing_usernames.keys():
        if username[0].lower() == k.lower():
            print('Username taken, prompting for new entry.')
            return None
        else:
            print('Username available.')
            return username
Reply
#3
(May-12-2017, 03:43 PM)Low_Ki_ Wrote:
    for k in existing_usernames.keys():
        if username[0].lower() == k.lower():
            print('Username taken, prompting for new entry.')
            return None
        else:
            print('Username available.')
            return username
Why is that a for loop, if you always return in the first iteration? Or is it an error that you currently only check the first username?
Reply
#4
(May-12-2017, 04:00 PM)nilamo Wrote:
(May-12-2017, 03:43 PM)Low_Ki_ Wrote:
    for k in existing_usernames.keys():
        if username[0].lower() == k.lower():
            print('Username taken, prompting for new entry.')
            return None
        else:
            print('Username available.')
            return username
Why is that a for loop, if you always return in the first iteration?  Or is it an error that you currently only check the first username?

You're right. I should use the
if any()
just like the above check... Thanks for noticing that!

I wrote this... still not working correctly... could I get some help with this block?

if any(k.lower() in existing_usernames.keys() for k in username[0].lower()):
        print('Username taken')
        return None
    else:
        print('Username available')
        return username

if any(k in existing_usernames.keys() for k in username):
        print('Username taken')
        return None
    else:
        print('Username available')
        return username
works... but where would I add the .lower() function to check username properly?
Reply
#5
Add it right where you need it... if any(k.lower()
Reply
#6
(May-12-2017, 04:57 PM)nilamo Wrote: Add it right where you need it... if any(k.lower()

You're awesome nilamo! Thank you sir
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Incomplete Output oldcity 6 3,711 Oct-21-2018, 07:08 PM
Last Post: oldcity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020