2012-07-08 178 views
0

如果用戶輸入'否',程序將不會通過for循環。如果用戶輸入'no',我不希望它tmpfile.write(行)。如何結束while循環並跳過for循環?

def remove(): 
    coname = raw_input('What company do you want to remove? ') # company name 
    f = open('codilist.txt') 
    tmpfile = open('codilist.tmp', 'w') 
    for line in f: 
     if coname.upper() in line: 
      while True: 
       answer = raw_input('Are you sure you want to remove ' + line.upper() + '?') 
       if answer == 'yes': 
        print line.upper() + '...has been removed.'    
       elif answer == 'no': 
        break # HERE IS WHERE I NEED HELP 
       else: 
        print 'Please choose yes or no.'     
     else: 
      tmpfile.write(line) 
    else: 
     print 'Company name is not listed.' 
    f.close() 
    tmpfile.close() 
    os.rename('codilist.tmp', 'codilist.txt') 
+0

這是GOTO合法的一種情況。 – 2012-07-08 07:11:06

回答

3

設置一個標誌變量,然後跳出while循環。然後在for循環中,檢查標誌是否設置,然後中斷。

PS:如果是循環

+2

最後的'else'附加到'for' - 如果循環沒有「break」,它將觸發。 – lvc 2012-07-08 07:27:02

+0

檢查文檔...'for ... else'是合法的。 – 2012-07-08 07:27:30

+0

@ lvc,喬爾Cornett ..感謝..這是我的新:) – vaisakh 2012-07-08 07:34:08

0

你必須把整個for循環的功能和使用return擺脫它:

def find_and_remove(f,coname,tmpfile): 
    for line in f: 
     if coname.upper() in line: 
      while True: 
       answer = raw_input('Are you sure you want to remove ' + line.upper() + '?') 
       if answer == 'yes': 
        print line.upper() + '...has been removed.'    
       elif answer == 'no': 
        return # HERE IS WHERE I NEED HELP 
       else: 
        print 'Please choose yes or no.'     
     else: 
      tmpfile.write(line) 
    else: 
     print 'Company name is not listed.' 

def remove(): 
    coname = raw_input('What company do you want to remove? ') # company name 
    f = open('codilist.txt') 
    tmpfile = open('codilist.tmp', 'w') 
    find_and_remove(f,coname,tmpfile) 
    f.close() 
    tmpfile.close() 
    os.rename('codilist.tmp', 'codilist.txt') 
2

要做到這一點最簡單的方法是創建一個獲取用戶輸入的函數:

def get_yes_or_no(message): 
    while True: 
     user_in = raw_input(message).lower() 
     if user_in in ("yes", "no"): 
      return user_in 

並修改您的原始功能,如下所示:

def remove(): 
    coname = raw_input('What company do you want to remove? ') # company name 
    f = open('codilist.txt') 
    tmpfile = open('codilist.tmp', 'w') 
    for line in f: 
     if coname.upper() in line: 
      answer = get_yes_or_no('Are you sure you want to remove ' + line.upper() + '?') 
      #answer logic goes here    
     else: 
      tmpfile.write(line) 
    else: 
     print 'Company name is not listed.' 
    f.close() 
    tmpfile.close() 
    os.rename('codilist.tmp', 'codilist.txt') 
+0

+1000作爲正確的代碼組織。 – 2012-07-08 08:38:36

0

而不是使用一個無限循環和break荷蘭國際集團,當你跳行,這三種情況(跳過,刪除和無效的答覆)在循環條件使用標誌區分。您將該標誌設置爲退出跳轉案例中的循環,中止刪除案例,並將該標誌按原樣保留在無效答案中。這使您可以使用whileelse子句(如果while由於條件變爲false而退出,則會觸發該子句)檢測跳過事例。從那裏,你可以跳到for循環的下一個迭代,使用continue(或者跳過所有其餘的行使用break - 從你打算的問題來看,這不是很清楚,但區別在於關鍵字的變化) :

for line in f: 
    if coname.upper() in line: 
     answered = False 
     while not answered: 
      answer = raw_input('Are you sure you want to remove ' + line.upper() + '?') 
      if answer == 'yes': 
       print line.upper() + '...has been removed.' 
       break 
      elif answer == 'no': 
       answered = True # HERE IS WHERE I NEED HELP 
      else: 
       print 'Please choose yes or no.' 
     else: 
      continue 
    else: 
     tmpfile.write(line) 
else: 
    print 'Company name is not listed.' 
1

Python有例外,您可以使用它來代替GOTO類型的構造。

class Breakout(Exception): 
    pass 

def remove(): 
    coname = raw_input('What company do you want to remove? ') # company name 
    f = open('codilist.txt') 
    tmpfile = open('codilist.tmp', 'w') 
    try: 
     for line in f: 
      if coname.upper() in line: 
       while True: 
        answer = raw_input('Are you sure you want to remove ' + line.upper() + '?') 
        if answer == 'yes': 
         print line.upper() + '...has been removed.' 
        elif answer == 'no': 
         raise Breakout() 
        else: 
         print 'Please choose yes or no.' 
      else: 
       tmpfile.write(line) 
     else: 
      print 'Company name is not listed.' 
    except Breakout: 
     pass 

    f.close() 
    tmpfile.close() 
    os.rename('codilist.tmp', 'codilist.txt') 

請注意,在那裏中間引發異常。