Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes and Functions
#21
(Apr-01-2017, 05:15 AM)wavic Wrote: I was thinking that you want the user to enter the username again after hitting the "Enter" button if thoughts.

While True:
    if prompt_username in names:
        print('The user exists!')
        prompt_username = input(prompt_message)
    else:
        break

Hey, that code actually works perfect! Thank you so much for that. I figured out another way I could do this as well. As far as creating a file if it does not exist already... Thanks for the while loops I don't know why I didn't think to flag it True.
I could probably modify and implement this into my code and use a dict() to read in, check, and append user info to. Username:Password
Example of opening and saving user data:

import json

def greet_user():
    filename = 'username.json'

    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except FileNotFoundError:
        with open(filename, 'w') as f_obj:
            username = input("What is your name? ")
            json.dump(username, f_obj)
            print("We'll remember you when you come back, " + username + "!")
    else:
        print("Welcome back, " + username + "!")

greet_user()

Another good example would be to add user info like such:

import json

def get_stored_username():
    filename = 'username.json'
    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return username



def greet_user():
    username = get_stored_username()
    if username:
        print("Welcome back, " + username + "!")
    else:
        username = input("What is your name? ")
        filename = 'username.json'
        with open(filename, 'w') as f_obj:
            json.dump(f_obj)
            print("We'll remember you when you come back, " + username + "!")

greet_user()
Reply
#22
Don't forget to hash the passwords. Look at bcrypt
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#23
(Apr-01-2017, 08:56 PM)wavic Wrote: Don't forget to hash the passwords. Look at bcrypt

Ok will do. Thanks for the help. It was greatly appreciated my friend! Hopefully one day I'll be able to provide help to the programming community just like you guys do. wavic / Larz :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,468 Aug-25-2020, 03:43 AM
Last Post: micseydel
  How can classes access each other Functions and Variables at the same time PythonOK 4 2,991 Dec-09-2018, 03:46 AM
Last Post: ichabod801
  Using classes? Can I just use classes to structure code? muteboy 5 4,981 Nov-01-2017, 04:20 PM
Last Post: metulburr
  Python Classes or Functions for large scale application ? Vithulan 5 4,533 Oct-23-2017, 04:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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