2017-09-23 11 views
-1
として
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の初心者 - 文が等しい

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

それを再気軽に...ベースとして使用何かで、おそらくありますその末尾に改行文字があるので、それらの文字列*は等しくありません。 – jonrsharpe

答えて

0

以下の作品と私は定期的に `プリント(のrepr(passwordline))`試してみてください、あなたのニーズに目的に

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()