如何将特定用户选择放入循环列表中

时间:2015-11-19 21:58:57

标签: python python-3.x

我正在尝试获取我的python代码,所以它就像这样发生 进口Register.txt [完成] 现在循环它并询问用户想要去哪个变量 以下代码是:

print("Registration System")
print("Make sure their is atleast one name in register!")
input("Enter any key to continue ")
with open ("register.txt", "r") as register:
    registers = [line.rstrip("\n") for line in register]
    for pop in registers:
        print(pop)

谢谢!

1 个答案:

答案 0 :(得分:2)

如果我理解你,你可以试试这个:

print("Registration System")
print("Make sure their is atleast one name in register!")
input("Enter any key to continue ")
set1 = set()
set2 = set()
with open("register.txt", "r") as register:
    registers = [line.rstrip("\n") for line in register]
    for pop in registers:
        print(pop)
        choice = input(
            'Where would you like it to go? (Enter 1 - set1, enter 2 - set2)')
        if choice == '1':
            set1.add(pop)
        elif choice == '2':
            set2.add(pop)

print(set1)
print(set2)