我如何输入固定的用户名和密码,而不需要在python中输入字段?

问题描述 投票:0回答:1

我有一个python脚本,提示输入用户名和密码。这可以正常工作,但是我想知道是否可以在文件中预设固定的用户名和密码。有什么方法可以轻松地将下面的密码提示编辑为用户名和密码的固定条目? s

    username = input("Enter the username: ")
    password = input("Enter the password: ")
python input passwords username
1个回答
0
投票

我认为最好的方法听起来像是函数的默认参数。类似于:

def log_me_in(username=None, password=None):
    if username is None:
        username = input("Enter the username:\n>>> ")
    if password is None:
        password = input("Enter the password:\n>>> ")
    print("Logging in user {} with password {}".format(username, password))

这使您可以灵活地运行log_me_in("David", "$omething$ecret"),而不必每次都输入这些参数。或者,您可以省略这些参数,并使用默认的None值(强制您在程序执行期间键入输入)。

示例:

log_me_in()

Enter the username:
>>>Dave
Enter the password:
>>>pa$$word
Logging in user Dave with password pa$$word

log_me_in(“ Dave”)

Enter the password:
>>> My Password
Logging in user Dave with password My Password

log_me_in(“ Dave”,“ Password”)

Logging in user Dave with password Password

log_me_in(password =“ A secret”)

Enter the username:
>>> Dave
Logging in user Dave with password A secret
© www.soinside.com 2019 - 2024. All rights reserved.