channel_total_list = []
get_channel_entry = int(raw_input('How many channels do you want to delete? '))
if get_channel_entry > 0:
while True:
user_channel_number = int(re.sub('\D', '', raw_input("Enter a channel number, (3d): "))[:3]);
channel_total_list.append(user_channel_number)
get_channel_entry = get_channel_entry - 1
print channel_total_list
我正在尝试从用户输入获取频道号码。如果他们第二次输入相同的号码,我想请用户输入不同的号码。
我怎样才能做到这一点?
答案 0 :(得分:1)
由于您要将用户的输入附加到channel_total_list
,因此您可以使用if user_channel_number in channel_total_list
检查输入是否已在列表中。
答案 1 :(得分:0)
你只需要在中间添加一个if语句就可以了。 以下是一些经过修改的代码,"而#34;在以前的情况下,它没有工作,因为它无限。
channel_total_list = []
get_channel_entry = int(raw_input('How many channels do you want to delete? '))
leave = False
while not leave:
user_channel_number = int(re.sub('\D', '', raw_input("Enter a channel number, (3d): "))[:3])
if user_channel_number not in channel_total_list:
channel_total_list.append(user_channel_number)
get_channel_entry = get_channel_entry - 1
if get_channel_entry == 0:
leave = True
print channel_total_list