2013-01-09 128 views
1

我有一個python腳本,我提供所有輸入一次,然後打印一些輸出。現在,我希望是這種格式python腳本暫停,如何禁用它?

  input 1 
      input 2 
      input 3 
      output 1 
      output 2 

我現在的問題是,當我運行python腳本,並打印「輸出1」後,提供輸入(終端粘貼)的輸出停頓和我按Enter鍵再次打印。

from sets import Set 

test_cases = int(raw_input()) 

while test_cases > 0: 
    cats_vs_dogs = {} 
    input_ = raw_input() 
    input_ = input_.split(" ") 
    total_cats = input_[0] 
    total_dogs = input_[1] 
    votes = int(input_[2])  
    remove_count = 0 
    cats = Set() 
    dogs = Set() 
    for i in range(1, int(total_cats) + 1): 
     cats.add('C' + str(i)) 
    for i in range(1, int(total_dogs) + 1): 
     dogs.add('D' + str(i)) 

    for i in range(0, votes): 
     input_ = raw_input() 
     input_ = input_.split(" ") 
     cat = input_[0] 
     dog = input_[1]   
     if cat in cats: 
      cats.remove(cat) 
     elif cat in dogs: 
      dogs.remove(cat) 
     if dog in cats: 
      cats.remove(dog) 
     elif dog in dogs: 
      dogs.remove(dog) 

     if dog not in cats_vs_dogs: 
      # now simply add the vote 
      if cat in cats_vs_dogs: 
       temp = list(cats_vs_dogs[cat]) 
       temp.append(dog) 
       cats_vs_dogs[cat] = temp 
      else: 
       # cat was not found 
       cats_vs_dogs[cat] = list([dog]) 
     else: 
      # relevant dog is present, remove corresponding cat 
      dogs_list = list(cats_vs_dogs[dog]) 
      if cat in dogs_list: 
       # remove this entry and don't save it 
       dogs_list.remove(cat) 
       cats_vs_dogs[dog] = dogs_list 
       remove_count += 1 
      else: 
       # no reverse entry exists so simply add 
       if cat not in cats: 
        cats_vs_dogs[cat] = list([dog]) 
       else: 
        cats_list = list(cats_vs_dogs[cat]) 
        cats_list.append(dog) 
        cats_vs_dogs[cat] = cats_list 


    # now update the count 
    for k in cats_vs_dogs.keys(): 
     remove_count += len(cats_vs_dogs[k]) 
    print remove_count + len(cats) + len(dogs) 
    # print 'cats: ', cats 
    # print 'dogs: ', dogs 
    test_cases -= 1 
    cats.clear() 
    dogs.clear() 
    cats_vs_dogs.clear() 

i電源作爲輸入的是:

2 
    1 1 2 
    C1 D1 
    D1 C1 
    1 2 4 
    C1 D1 
    C1 D1 
    C1 D2 
    D2 C1 

輸出,我得到的是:

... 
    D2 C11 
    <system pause> I press enter 
    3 

,而我想這是因爲

D2 C1<last line> 
    1 
    3 

回答

0

嗯,我試圖弄清楚你的代碼到底是做什麼的,但我可以提供af EW建議你可以嘗試:

  • 當您使用range()你不需要放於零,所以range(0,6)是一樣的range (6)
  • 當您使用raw_input()時,通常會包含某種提示。這對調試也很有幫助。這樣你就可以知道你所看到的停頓是否真的在等待輸入。一個例子是test_cases = raw_input("Enter number of test cases: ")
  • 最後,關於您的實際問題:您的print聲明包含在while區塊中。所以它可能是打印一些東西,然後回到整個代碼。嘗試將它從塊中拉出來,看看會發生什麼。