【问题标题】:Enter and Save username and password into same file输入并保存用户名和密码到同一个文件
【发布时间】:2021-12-22 12:54:29
【问题描述】:

我希望用户能够输入新的用户名和密码并将其保存到同一个文件中,以便用于随后的登录过程。用户应该能够在input 中输入新的用户名,密码也是如此,然后将其保存到文件中以供以后使用。我自己会这样做,但我不知道如何将它保存到同一个文件中。

import time
import sys

uun = "test"
uun01 = "test01"
uun02 = "test02"
usernames = (uun+uun01+uun02)

upw = "test"
upw01 = "test01"
upw02 = "test02"
passwords = (upw+upw01+upw02)

max_attempts = 3
attempts = 0

while True:
    print("Username")
    username = input("")

    print("Password")
    password = input("")

    if username in usernames and password in passwords:
        print("Access Granted")
    else:
        attempts+=1
        if attempts >= max_attempts:
            print(f"reached max attempts of {attempts} ")
            sys.exit()
        print("Try Again (10 sec)")
        time.sleep(10)
        continue
    break

【问题讨论】:

    标签: python passwords


    【解决方案1】:

    在决定如何归档数据时,我要做的第一件事就是考虑以后要如何访问它——这决定了数据结构。几个例子是,直接键:值字典(用户名:密码)按用户名搜索,或者输入时间:(用户名,密码)按何时进行搜索。

    一旦确定,格式化为字符串并写入文件。数据结构的简单 print() 将显示它“原始”的外观以及您可能希望如何格式化它以便以后访问。

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 2019-06-11
      • 2020-07-19
      • 2015-04-05
      • 1970-01-01
      相关资源
      最近更新 更多