2015-05-07 156 views
-2

这是我遇到问题的循环,并且我使用的是我在其他问题中使用的同一个CSV文件:为什么我的打印语句重复两次?

我正在分析关于投诉ID的60MB CSV文件,对某些公司的投诉。我询问用户他/她想要学习的日期,并且当他/她输入日期时,我的程序应该产生一个具有相应ID列表的单行代码。

基本上,input_IDs是ID号列表,input_dates是与每个ID相关的日期列表(以月/日/年格式)。

matching_list = [] 


Date_Input = input("First, input a date in this format, 'Month/Day/Year.' ->  Type in 'Quit' if you are done. : ") 

if Date_Input == "Quit": 
    print ("Too bad you quit so early!") 

for i in range(len(input_dates)): 
    if input_dates[i] == Date_Input: 
     matching_list.append(input_IDs[i]) 

if len(matching_list) > 0: 
    print ("Here is the list of complaint IDs you can choose from and ask about :", matching_list) 

while True: 
    new_match_list = [] 

    if len(matching_list) == 0: 
    Date_Input = input ("There are no complaint IDs available for that date! Enter another date: ") 

    for x in range(len(input_dates)): 
     if input_dates[x] == Date_Input: 
      new_match_list.append(input_IDs[x]) 

    if len(new_match_list)>0: 
     print ("Here is the list of complaint IDs you can choose from and ask about :", new_match_list) 
     break  

的代码产生上述标识正确的名单,但会产生两个副本,而不是只有一个,:比方说,我想这发生在 2015年4月23日ID列表,

First, input a date in this format, 'Month/Day/Year.' -> Type in 'Quit' if you are done. : 4/23/2015 
Here is the list of complaint IDs you can choose from and ask about : ['1344139', '1344055', '1343332', '1343188', '1343131', '1341190', '1340441', '1338003', '1336832', '1329966', '1301958', '1251144'] 
Here is the list of complaint IDs you can choose from and ask about : ['1344139', '1344055', '1343332', '1343188', '1343131', '1341190', '1340441', '1338003', '1336832', '1329966', '1301958', '1251144'] 

为什么有两个打印语句正在生成,而不仅仅是一个?

+1

“这是。”'代码语句:您可以通过while之前移除印刷区域解决这个问题,只是把它在循环的开始。 – dbliss

回答

1

您有两个打印语句 - 一个在循环之前,一个在其结尾。因为你有两个`

while True: 
    if len(new_match_list)>0: 
     print ("Here is the list of complaint IDs you can choose from and ask about :", new_match_list) 
     break 

    new_match_list = [] 

    if len(matching_list) == 0: 
    Date_Input = input ("There are no complaint IDs available for that date! Enter another date: ") 

    for x in range(len(input_dates)): 
     if input_dates[x] == Date_Input: 
      new_match_list.append(input_IDs[x])