2015-12-14 49 views
-2

的名单这是我对我的注册/登录程序当前的代码,我与它有一个问题:我试图alsorts但输入将不被记录,因为我转换列表中的元组

info = [] 
n = 0 
true = "true" 

while true == "true": 
    command = input("What would you like to do?\nLogin or Signup? ") 
    if "ogin" in command: 
     loginusername = input("\nWhat is your username? ") 
     loginpassword = input("What is your password? ") 
     if (loginusername and loginpassword) in info: 
      print("\nYou have logged in!\n")   
     else: 
      print("\nYou have not signed up!\n") 
    if "ignup" in command: 
     usernameinput = input("\nWhat do you want your username to be? ") 
     info.insert(n, usernameinput) 
     usernameinput = "false" 
     passwordinput = input("\nWhat do you want your password to be? ") 
     info.insert(n, passwordinput) 
     passwordinput = "false" 
     print("\n\nYou have successfully signed up!") 
     n += 1 

希望它是。我想它要记录这样为例:

info == [("username1", "password1")] 

有没有一种方法可以让我记录下来这样的,因为在目前,该代码记录了这样的数据:

info == ["username1", "password1"] 
+0

当然,但用户应该放入什么?名称和密码? – timgeb

+0

请现在添加[mcve] RIght,我不确定你想要做什么。 –

+0

@TomGolledge:编辑过的版本是否也符合你想要达到的目标?看起来这是你需要的 - 从一个数据结构转换到另一个数据结构。 – Tadeck

回答

0

我建议你提示用户两次,输入用户名和密码。例如:

uname = input('enter username: ') 
pw = input('enter password: ') 
result = [(uname, pw)] # this is the result you requested 
print(result) 

演示:

$ python3 inputdemo.py 
enter username: bob 
enter password: pw  
[('bob', 'pw')] 

编辑:转换已收集的数据,你也可以做到以下几点:

user, password = user_input 
result = [(user, password)] 

或以下:

result = [tuple(user_input)] 

user_input)从list转换为tuple