2017-10-28 79 views
0
print ("Welcome to my Quiz!") 
existing = input("Are you an existing user?: ") 
if existing.lower == "yes": 
    print("Enter your credidentials") 
username= input("Enter your Username: ") 
password= input("Enter your Password: ") 
file= open("data.txt", "r") 
found=False 
for line in file: 
    account = line.split(",") 
    if account[0] == username: 
     password= existing[1] 
     found=True 
file.close() 
if found==True: 
    print("Welcome Back", username ,) 
if found==False: 
    print("Account not found") 
else: 
    existing.lower == "no" 
    user= input("Enter your first name: ") 
    year= input("Enter the year you are in: ") 
    password= input("Enter your password: ") 
    username=user[:2]+year  
    writefile=open("data.txt","a") 
    writefile.write(username + "," + password + "\n") 
    writefile.close() 
    print("Your account has been created." "Your username is..", username , "..and your password is", password,) 

我试图做一个测验,我希望它的工作原理是,如果他们的用户有一个帐户,它不会经历一个新的帐户的过程。现在,如果我登录,它仍然要求我的名字等,完全跳过ifelse命令。跳过如果和其他命令

+0

'existing.lower'应该是'existing.lower()',但除非你有一些严重的压痕问题,这ISN” t相关。 – chepner

+0

也无关紧要,你永远不会检查输入的密码是* correct *密码。 – chepner

+0

您需要缩进从'username ='一直到'print(“Account not found”)的所有内容。因为'else'语句是第一个'if',所以在顶部 – abccd

回答

0

试试这个:

print("Welcome to my Quiz!") 

existing = input("Are you an existing user?: ") 


if existing.lower() == "yes": 
    print("Enter your Credidentials") 

    username = input("Enter your Username: ") 
    password = input("Enter your Password: ") 

    with open("data.txt", "r") as raw_data: 
     my_data = raw_data.read() 

    re_data = my_data.split('\n') 

    check_box = username + "," + password 

    if check_box in re_data: 
     print("Welcome Back " + username) 

    else: 
     print("Account not found") 


else: 

    user = input("Enter your first name: ") 
    year = input("Enter the year you are in: ") 
    password = input("Enter your password: ") 
    username = user[:2]+year 

    save_box = username + "," + password + "\n" 

    with open("data.txt", "a") as raw_data: 
     raw_data.write(save_box) 

    print("Your account has been created." "Your username is.." + username + "..and your password is" + password) 


print("Goodbye, Have a Nice Day ...") 

您username_password文件应该是:

jack,6354734346534 
edward,45645646754 
jeje,874574574587 
+0

感谢您的帮助。当我登录,虽然说,帐户没有找到x3然后说欢迎回来,用户名,然后另一个'帐户未找到' –

+0

oohhhh yeahhhh我忘记了!我会更新它... – DRPK

+0

@亚当:现在它可以... – DRPK

1

两个问题是有

  1. 如果其他对准问题以及
  2. 调用低函数
  3. 读线(未剥换行字符)

试试这个,并提供反馈

print ("Welcome to my Quiz!") 
existing = input("Are you an existing user?: ") 
if existing.lower()[0] == "y": 
    print("Enter your credidentials") 
    username= input("Enter your Username: ") 
    password= input("Enter your Password: ") 
    file= open("data.txt", "r") 
    found=False 
    for line in file: 
     account = line.strip().split(",") 
     if account[0] == username: 
      password= account[1] 
      found=True 
    file.close() 
    if found==True: 
     print("Welcome Back", username) 
    else: 
     print("Account not found") 
else: 
    #print("Account not found") 
    #existing.lower() == "no" 
    user= input("Enter your first name: ") 
    year= input("Enter the year you are in: ") 
    password= input("Enter your password: ") 
    username=user[:2]+year  
    writefile=open("data.txt","a") 
    writefile.write(username + "," + password + "\n") 
    writefile.close() 
    print("Your account has been created." "Your username is..", username , "..and your password is", password,)