程序为什么不能比较用户输入和文件?

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

我看不到我的程序无法比较用户输入等的原因。我的程序是我正在使用的操作系统的用户名/密码程序,我不确定为什么它无法正常工作,即使它是正确的,它也总是会以错误的方式返回?

import os
isuserempty = os.stat("Username.txt").st_size == 0
ispassempty = os.stat("Password.txt").st_size == 0
if isuserempty or ispassempty == True:
    print("Hello, new user create a username and password.")
    newusername = input("Enter your new username: ")
    NewUser = open("Username.txt" , "a")
    NewUser.write(newusername)
    NewUser.close()
    newpassword = input("Enter your new password: ")
    NewPass = open("Password.txt" , "a")
    NewPass.write(newpassword)
    NewPass.close()
    print("You now have a new Username and Password!")
    print("You are now logged in!")
elif isuserempty or ispassempty == False:
    truePass = open("Password.txt", "r")
    trueUser = open("Username.txt", "r")
    print("Enter your Username.")
    userUserName = input("Enter:")
    print("Enter your password.")
    userPass = input("Enter:")
    if userUserName == trueUser and userPass == truePass:
        print("Correct! Logging in...")
    if userUserName != trueUser or userPass != truePass:
        print("Incorrect!")
python
1个回答
0
投票

您的truePasstrueUser是当前与字符串进行比较的TextIOWrapper类型的变量(文件)。在进行比较之前,请在elif块中读取文件的内容。

pass_file = open("Password.txt", "r")
truePass=pass_file.read()
user_file = open("Username.txt", "r")
trueUser = user_file.read()
© www.soinside.com 2019 - 2024. All rights reserved.