Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么我的程序会打印;无效的用户名或密码";当它';是的_Python 3.x_Login - Fatal编程技术网

Python 3.x 为什么我的程序会打印;无效的用户名或密码";当它';是的

Python 3.x 为什么我的程序会打印;无效的用户名或密码";当它';是的,python-3.x,login,Python 3.x,Login,我尝试了许多不同的输入,但他们都说打印“无效”。如果有人能解释或编辑我的代码以便我理解,我将不胜感激 username = [] password = [] def account_creation(): account = input("Would you like to make an Account? Yes or No: ") if account == "Yes" or "yes": username = input("Please make a Us

我尝试了许多不同的输入,但他们都说打印“无效”。如果有人能解释或编辑我的代码以便我理解,我将不胜感激

username = []
password = []

def account_creation():
    account = input("Would you like to make an Account? Yes or No: ")
    if account == "Yes" or "yes":
        username = input("Please make a Username: ")
        password = input("Please make a Password: ")
    else:
        return
account_creation()

def login():
    account = input("Do you have an Account? Yes or No: ")
    if account == "Yes":
        login_username = input("Plese enter your username: ")
        login_password = input("Plese enter your password: ")
    else:
        return
    if login_username and login_password == username and password:
        print("Welcome back")
    else:
        print("Invalid Username or Password")

login()

您需要首先检查用户名密码检查行(如文基所述):

但你这里缺少了更重要的东西。 您试图更改函数中的全局变量,因此需要在函数体中包含相关变量的
global
关键字

def account_creation():
    global username,password
    account = ... the rest of the code

如果
login username==True
login\u password==username
password==True
,您的代码将打印“欢迎回来”。应该是
login\u username==username和login\u password==password
可能是重复的感谢您的澄清,这让我很困惑,但现在我明白我错在哪里了。您能澄清一下我需要在哪里添加每个新的部分吗?我有点困惑。所以我理解全局变量的需要,但我需要在用户名密码检查行中检查什么?venky已经给出了这一部分,但我认为我也应该在这里添加这一部分。我猜你是在我编辑的时候写的。我已经做了用户名密码检查行和全局变量,但它似乎仍然打印了无效的用户名或密码,我还做了多个测试。我确信你在尝试更正时在其他地方出错了。只要在你的问题中复制粘贴你自己的代码,然后修补我给你的部分。我只改变了那些部分,它肯定能工作。如果你在linux上,我建议你做一个差异测试,看看事情进展如何wrong@Owain1105如果Yilmaz的回答对你有帮助,请接受。
def account_creation():
    global username,password
    account = ... the rest of the code