我正在尝试在 python 中为一些家庭作业创建一个注册和登录系统。虽然它没有按预期工作

    ExistingUser = input("Please enter either Y or N: ")


if (ExistingUser) == "N" or "n":

    newName = input("Please enter your name: ")

    with open("Userdata.txt", "a") as f:

        print(newName, file = f)

    newUserID = input("Please enter your desired username: ")

    with open("Userdata.txt", "a") as f:

        print(newUserID, file = f)

    newPassword = input("Please enter your desired password: ")

    with open("Userdata.txt", "a") as f:

        print(newPassword, file = f)

        f.close()


elif (ExistingUser) == "Y" or "y":

    existingUserID = input("Please enter your username: ")

    with open("Userdata.txt") as f:

        if (ExistingUserID) in f.read():

            print("True")

当我输入 Y 或 y 时,它应该会触发登录过程。相反,它会触发注册。


慕姐4208626
浏览 145回答 2
2回答

拉莫斯之舞

if (ExistingUser) == "N" or "n":if (ExistingUser) == "N" or True:与您的 if 条件相同True 有关更多信息 - 阅读真值测试采用if ExistingUser in ('N', 'n'):或者if ExistingUser.lower() == 'n':请注意,不需要在 . 周围使用括号ExistingUser。

LEATH

在我看来,您的 If 语句不正确 如果您将它们更改为这种格式,它似乎可以工作if (ExistingUser == "N" 或 ExistingUser == "n"):elif (ExistingUser == "Y" 或 ExistingUser == "y"):采用这种格式: if (ExistingUser) == "N" or "n":'or "n"' 总是计算为真
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python