我看不到我的程序无法比较用户输入等的原因。我的程序是我正在使用的操作系统的用户名/密码程序,我不确定为什么它无法正常工作,即使它是正确的,它也总是会以错误的方式返回?
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!")
您的truePass
和trueUser
是当前与字符串进行比较的TextIOWrapper
类型的变量(文件)。在进行比较之前,请在elif
块中读取文件的内容。
pass_file = open("Password.txt", "r")
truePass=pass_file.read()
user_file = open("Username.txt", "r")
trueUser = user_file.read()