I want to make a game that allows users to make a log in which requires them to make a username and password,I will then give them an option whether they woul like to register or log in.This is where I am stuggling as I dont know how to save their log in and then make it re appear when they choose to register with their log in.I dont know if this makes sense but it would really help if you could suggest a solution,i have inserted a bit of my code.

   name=input("Please enter your name ")
    age=input("Please enter your age ")
    print("Hello" + name)
    username=name+age
    print("Your username is" +username)

Dani AI

Generated

You are on the right track, . The missing piece is persistence and safe password storage. Lists (like in ’s demo) disappear when the program exits, and checking passwords by equality is not secure. As hinted, a tiny SQL database is a good fit. The recipe below uses SQLite (built in), salts, and PBKDF2 so you never store raw passwords.

import sqlite3, secrets, hashlib, hmac

con = sqlite3.connect('game.db')
con.execute('CREATE TABLE IF NOT EXISTS users(u TEXT PRIMARY KEY, s BLOB NOT NULL, h BLOB NOT NULL)')

def _hash(pwd, salt=None, rounds=200_000):
    salt = salt or secrets.token_bytes(16)
    digest = hashlib.pbkdf2_hmac('sha256', pwd.encode('utf-8'), salt, rounds)
    return salt, digest

def register(username, password):
    salt, digest = _hash(password)
    try:
        with con:
            con.execute('INSERT INTO users(u,s,h) VALUES(?,?,?)', (username, salt, digest))
        return True
    except sqlite3.IntegrityError:  # username already exists
        return False

def verify(username, password):
    row = con.execute('SELECT s,h FROM users WHERE u=?', (username,)).fetchone()
    if not row:
        return False
    salt, stored = row
    _, digest = _hash(password, salt)
    return hmac.compare_digest(stored, digest)

How to use it in your flow:

  • At startup, show a simple menu: Register or Login.
  • On Register: ask for a username the player chooses (do not build it from name+age), then call register(...). If False, tell them it is taken.
  • On Login: call verify(...) and continue only if it returns True.

Tips:

  • Hide password input with getpass.getpass().
  • Always use parameterized SQL (as shown) to avoid injection.
  • For online or multi-user games, consider dedicated password libraries (bcrypt/argon2) and rate limiting, but the above is a solid baseline for a local game.

Recommended Answers

All 2 Replies

name = ['shiv','deepak','baldev','shishupal','raj','depanshu','balram','shivani','shweta','deepika','manisha','shruti']
email = ['shiv123','deepak123','baldev123','shishupal123','raj123','depanshu123','balram123','shivani123','shweta123','deepika123','manisha123','shruti123']
psk = ['shiv123','deepak123','baldev123','shishupal123','raj123','depanshu123','balram123','shivani123','shweta123','deepika123','manisha123','shruti123']

nam = input("Enter your name : ")

if nam in name :
  print ("Welcome "+nam+".")

  em = input("Please enter your email id : ")

  if em in email:
    print ("Welcome : " +em )

    passkey = input("Please enter your password : ")

    if passkey in psk :
      print ("Welcome "+nam+" Nice to meet you again...")

    else:
      print("Wrong Credential !")

  else:
      print("Wrong Credential !")

else:
      print("Wrong Credential !")

I have do this in python 3.6.1 version you can try.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.