二手产品经理

二手产品经理

THIS IS RENO

db - 72 days - Learn Python online for 100 days

Record#

  1. Modify the code for Day 62 to create an account password for it.
  2. When the code is first run, if there is no account password, create an account and password. The account password is stored in the replit db.
  3. When running again, the account password must be correct to access.
  4. There is still some confusion about the concept of db during the writing process, and there are some issues when dealing with key and value.
  5. It is difficult to understand the for loop, and it is not easy to quickly understand the looping process and results.

CODE#

from replit import db
import os, datetime, random, time

correctPassword = "1"


def add(t):
  db[timestamp] = t


def view():
  show = "n"
  for key in dblist:
    if show.lower() == "n" and key != "userinfo":
      print(f"{key}: {db[key]}")
      print("---")
    elif show.lower() == "e":
      break
    show = input("Next or Exit? > ")


while True:
  dblist = db.keys()
  if "userinfo" not in dblist:
    print("Set !")
    username = input("Username > ")
    password = input("password > ")
    salt = random.randint(1000, 9999)
    newpass = hash(f"{password}{salt}")
    db["userinfo"] = {"username": username, "password": newpass, "salt": salt}
  else:
    print("Input !")
    userinfo = db["userinfo"]
    username = input("Username > ")
    password = input("password > ")
    salt = userinfo["salt"]
    newpass = hash(f"{password}{salt}")
    if username == userinfo["username"] and userinfo["password"] == newpass:
      while True:
        os.system("clear")
        menu = input("Welcome\n1: Add\n2: View\n")
        if menu == "1":
          timestamp = datetime.datetime.now()
          print(timestamp)
          add(input("> "))
        elif menu == "2":
          view()
    else:
      print("username or password error!")
    time.sleep(3)


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.