python:从字典中提取信息

时间:2020-06-04 19:40:04

标签: python python-3.x dictionary if-statement input

教自己如何编码,我需要帮助

我正在尝试确保用户输入正确的用户名(credentials_U)和密码(credentials_P)

期望的输出

  • 如果他们俩都做对,那么它将打印出来
  • 如果他们只正确使用了用户名,则会输出错误的密码
  • 如果他们输入的用户名错误或密码错误,则会打印错误并再次循环。

当前输出 什么都不返回

directory = {"ash":"123",}

def logon():
credentials_U = input("please input your username: ")
credentials_P = input("please input you password: ")
access = False

while access != True:
    if credentials_U in directory:
        if credentials_P in directory:      #true and true
            print ("welcome")
            access = True
            break


    elif credentials_U in directory:
        if credentials_P not in directory:       #true and false
            print ("incorrect password")
            logon()


    elif credentials_U not in directory:
        if credentials_P in directory:       #false and true
            print ("incorrect")
            logon()


    elif credentials_U not in directory:
        if credentials_P not in directory:      #false and false
            print ("incorrect")
            logon()

    else:
        print("incorrect details")
        logon()

2 个答案:

答案 0 :(得分:3)

首先,您无法正确访问用户输入的字段的dict

第二,您正在使用递归,而不是使用已有的循环。

这应该更好:

directory = {"ash":"123",}

def logon():
    access = False
    while access != True:

        credentials_U = input("please input your username: ")
        credentials_P = input("please input you password: ")
        if credentials_U in directory:
            if credentials_P == directory[credentials_U]:      #true and true
                print ("welcome")
                access = True
            else:
                print ("incorrect password")
        else:
            print("incorrect details")

答案 1 :(得分:0)

您需要在所有位置进行更改 因为凭据_U将是密钥,而凭据_P将是值 检查以下代码,您需要在整个文件中进行更改

df = pd.read_csv(fp, dtype={"date":str})