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
我试图从用户输入中获取频道号。如果他们第二次输入相同的号码,我想请用户输入其他号码。
我该如何实现?
由于您将用户的输入附加到channel_total_list
,您可以检查输入是否已经在列表中使用if user_channel_number in channel_total_list
。
您只需要在中间添加if语句,就可以了。这是一些适合您的修改代码,"虽然"在以前的情况下没有工作,因为它将进入无限。
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