我在我的代碼中遇到問題。我創建了一個程序來創建一個帳戶,將用戶名和密碼保存在.txt文件中。然後請求登錄並檢查用戶名和密碼是否正確。但是每當其他條件執行時。我得到的結果是「你沒有任何賬戶」。請幫忙。提前致謝。錯誤在readline()或條件
# MyProgram: Account Verification
print "\ncreate account:\n"
f = open("data.txt",'w+')
def create():
user_name = raw_input("Enter username >> ")
password = raw_input("Enter password >> ")
confirm = raw_input("Enter password to confirm >> ")
if password == confirm:
f.write(user_name+"\n"+password)
f.close()
print "Account created"
else:
print "\nPassword not matched\n\n Enter details again:\n"
create()
create()
new = open("data.txt")
un = new.readline()
pw = new.readline()
new.close()
def login():
print "\nLogin:\n"
name = raw_input("Enter username >> ")
if name == un:
pas = raw_input("Enter password >> ")
if pas == pw:
print "Welcome!"
else:
print "Wrong password"
login()
else:
print "You don't have any account"
login()
我認爲你必須去掉''readline()'的輸出去掉換行符。 –
你能提供一個例子「data.txt」和導致問題的輸入嗎? –