将列表转换为元组列表

时间:2015-12-14 21:43:15

标签: python string python-3.x

这是我目前的注册/登录程序代码,我遇到了问题:

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"]

1 个答案:

答案 0 :(得分: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