2012-05-05 50 views
1

什麼是非常簡單的while循環語句是否會繼續下面的程序,直到用戶鍵入「exit」?簡單while循環直到Python中斷

例如,

while response = (!'exit') 
    continue file 
else 
    break 
    print ('Thank you, good bye!') 
#I know this is completely wrong, but it's a try! 

我至今文件:

#!/usr/bin/python 
friends = {'John' : {'phone' : '0401', 
     'birthday' : '31 July', 
     'address' : 'UK', 
     'interests' : ['a', 'b', 'c']}, 
    'Harry' : {'phone' : '0402', 
     'birthday' : '2 August', 
     'address' : 'Hungary', 
     'interests' : ['d', 'e', 'f']}} 
response = raw_input("Please enter search criteria, or type 'exit' to exit the program: ").split() 
try: 
    print "%s's %s is %s" % (response[0], response[1], friends[response[0]][response[1]]) 
except KeyError: 
    print "Sorry, I don't know about that. Please try again, or type 'exit' to leave the program: " 
+0

即我的主要問題是,是否有具體的說法,意思是「繼續運行該文件」 – user1374310

回答

3

while循環會一直持續到您所設置的條件是假的。所以你希望你的代碼主要在這個循環中。完成後,您知道用戶輸入了「退出」,以便您可以打印錯誤消息。

#!/usr/bin/python 
friends = {'John' : {'phone' : '0401', 
     'birthday' : '31 July', 
     'address' : 'UK', 
     'interests' : ['a', 'b', 'c']}, 
    'Harry' : {'phone' : '0402', 
     'birthday' : '2 August', 
     'address' : 'Hungary', 
     'interests' : ['d', 'e', 'f']}} 

response = [''] 
error_message = "Sorry, I don't know about that. Please try again, or type 'exit' to leave the program: " 

while response[0] != 'exit': 
    response = raw_input("Please enter search criteria, or type 'exit' to exit the program: ").split() 
    try: 
     print "%s's %s is %s" % (response[0], response[1], friends[response[0]][response[1]]) 
    except KeyError: 
     print error_message 
    except IndexError: 
     print error_message 

print ('Thank you, good bye!') 

此代碼是一個開始你想要什麼,但它仍然有一些缺陷。看看你是否可以重構它,以便當用戶輸入'exit'時不會打印錯誤消息。

4

continue是一個不需要參數的關鍵字。它只是告訴當前循環立即繼續進行下一次迭代。它可以在whilefor循環內使用。

然後,您的代碼應置於while循環中,該循環將繼續執行,直到滿足條件。您的條件語法不正確。它應該是while response != 'exit':。因爲您正在使用條件,所以不需要continue聲明。只要價值不是"exit",它將通過設計繼續。然後

你的結構是這樣的:

response = '' 
# this will loop until response is not "exit" 
while response != 'exit': 
    response = raw_input("foo") 

如果你想利用continue,它可能如果你打算做對響應其它各種操作使用,且可能需要停止早點再試一次。關鍵字break與循環中的行爲類似,但它表示我們應該立即完全結束循環。你可能有一些其他的條件是一個大忌:

while response != 'exit': 
    response = raw_input("foo") 

    # make various checks on the response value 
    # obviously "exit" is less than 10 chars, but these 
    # are just arbitrary examples 
    if len(response) < 10: 
     print "Must be greater than 10 characters!" 
     continue # this will try again 

    # otherwise 
    # do more stuff here 
    if response.isdigit(): 
     print "I hate numbers! Unacceptable! You are done." 
     break 
3
#!/usr/bin/python 
friends = { 
    'John' : { 
     'phone' : '0401', 
     'birthday' : '31 July', 
     'address' : 'UK', 
     'interests' : ['a', 'b', 'c'] 
    }, 
    'Harry' : { 
     'phone' : '0402', 
     'birthday' : '2 August', 
     'address' : 'Hungary', 
     'interests' : ['d', 'e', 'f'] 
    } 
} 

def main(): 
    while True: 
     res = raw_input("Please enter search criteria, or type 'exit' to exit the program: ") 
     if res=="exit": 
      break 
     else: 
      name,val = res.split() 
      if name not in friends: 
       print("I don't know anyone called {}".format(name)) 
      elif val not in friends[name]: 
       print("{} doesn't have a {}".format(name, val)) 
      else: 
       print("{}'s {} is {}".format(name, val, friends[name][val])) 

if __name__=="__main__": 
    main()