Python的初学者 - 即使这两个变量的值相同,这个if语句将不接受他们成为平等

问题描述:

task = input("would you like to register(a),take a test(b)or check you scores(c)?") 
if task == "a": 
    username = (input("what do you want as a username?") + input("what year were you born?")) 
    print("your user name is:"+username) 
    password = input("what do you want as a password?") 
    yeargroup = input("what year group are you in?") 
    with open(username+".txt","w") as file: 
     file.write(username) 
     file.write("\n") 
     file.write(password) 
     file.write("\n") 
     file.write(yeargroup) 

第一部分工作得很好,并写入正确Python的初学者 - 即使这两个变量的值相同,这个if语句将不接受他们成为平等

elif task == "b": 
    username = input("what is your username?") 
    with open(username+".txt", "r") as file: 
     lines=file.readlines() 
     password = input("what is your password?") 
     password = password.lower() 
     passwordline = lines[1] 
     print(password) 
     print(passwordline) 

我只是说这个文件部分是为了确保字符串的价值是相等的,下面的if语句仍然不起作用。

 if str(passwordline) == str(password): 
      print("welcome.") 
     else: 
      print("invalid password") 
+0

尝试'打印(再版(passwordline))',有可能它的末尾有一个换行符,所以这些字符串*不相等。 – jonrsharpe

以下的作品,亦是我作为一个基地经常使用......随意重新目的,您的需要

import time 
""" 
--------- 
Logged in 
--------- 
""" 
# Function for when the user succesfully logs in 
def LoggedIn(): 
    print("You are now logged in, since this has not completed we will log you out") 


""" 
--------- 
Menu 
--------- 
""" 
# The Menu 
def Menu(): 
# Welcomes the user 
    print("Hello and welcome to Smart Systems Account management system") 
# Asks the user if they want to login or create a account 
    choice = input("Would you like to login or create a account?") 
# if the user selects login 
    if choice.lower() == "login": 
# call the Login Function 
     Login() 
# if the user selects create 
    elif choice.lower() == "create": 
# call the create function 
     Create() 
# if the user inputs neither login or create 
    else: 
# Tells the user that what they inputted is incorrect 
# and tells them to type login or create 
     print("That is not a valid option") 
     print("Type 'Login' or 'Create'") 

""" 
--------- 
Create account 
--------- 
""" 
# Account creation function 
def Create(): 
# Open the Logins.txt file in append mode 
    file = open("Logins.txt", "a") 
# Ask user for their username 
    username = input("Input username") 
# ask user for their password 
    password = input("Input password") 
# asks user for their password again 
    password2 = input("Input password again") 
# If the two passwords match, are over or equal to 6 characters, Contains capital leters and isnot just alphabetical 
    if password == password2 and len(password) >= 6 and password != password.lower() and password.isalpha() == False: 
# save the users details to the file in a new line with a commer seperating them 
     file.write('\n'+username+","+password+",") 
# close the file 
     file.close() 
# tell the user account creation was a success 
     print("Account has been created") 
#take them back to the menu 
     Menu() 
# if the password does not match the requirements 
    else: 
# tell them the password is incorrect 
     print("Password does not have letters and numbers or Password does not have capitals or Password is not 6 or more letters long or Passwords do not match") 
# close the file 
     file.close() 
# take them back to the menu 
     Menu() 

""" 
--------- 
Login Code 
--------- 
""" 
# Login function 
def Login(): 
# Sets correct to false (Will explain below) 
    Correct = False 
# while correct = false then keep looping (Will be set to true when they get it correct) 
    while Correct == False: 
# open your file (Logins.txt is the file name in read mode (r)) 
     file = open("Logins.txt", "r") 
# ask for username 
     username = input("input username : ") 
# ask for password 
     password = input("input password : ") 
# for each line in Logins.txt 
     for line in file: 
# Split each word that is inbetween commers into seperate variables 
      details =line.split(",") 
# Details 0 is username and Details 1 is password 
# if username is same as file and password is same 
      if username == details[0] and password == details[1]: 
# set correct = true 
       Correct = True 
# set users name to login 
       login = details[0] 
# close file 
       file.close() 
# Break form the for loop 
       break 
# if its incorrect 
      else: 
# set a useless var to anything so it dosn't raise a error 
       pointlessvar = 1 
# if correct == True 
    if Correct == True: 
# Login user 
     LoggedIn() 
# else 
    else: 
# print incorrect 
     print("incorrect") 


""" 
--------- 
Main non function code 
--------- 
""" 
# create a continus loop 
while True == True: 
# call the choice function 
    Menu()