2015-05-07 155 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])