我在尝试获取 while 循环来验证用户输入,然后确保用户不重复值时遇到问题。下面显示的是我尝试过的两种方法,但我不知道如何让它们工作。
方法一
def test():
my_list = ["", "", ""]
for i in range(3):
while (my_list[i] != "one") and \
(my_list[i] != "two") and \
(my_list[i] != "three"):
while (my_list[i] == my_list[0]) and \
(my_list[i] == my_list[1]) and \
(my_list[i] == my_list[2]):
text = "Enter, one, two or three", i + 1, ":"
try:
my_list[i] = input(text)
except KeyboardInterrupt:
sys.exit()
print(my_list)
方法二
def test2():
my_list= ["", "", ""]
while len(my_list)!=len(set(my_list)) == True:
for c in range(4):
while (my_list[i] != "one") and \
(my_list[i] != "two") and \
(my_list[i] != "three"):
text = "Enter, one, two or three", c + 1, ":"
try:
my_list[c] = input(text)
except KeyboardInterrupt:
sys.exit()
print(my_list)