2013-02-10 133 views
0

我一直在試圖用Python做一個對數計算器。我離完成它只有一步之遙。下面是代碼:如果循環中存在循環,如何中斷程序?

import math 
print("Welcome to logarithm calculator") 

while True: 
    try: 
     inlog = int(input("Enter any value greater than zero to lookup its logarithm to the base 10\n")) 
     outlog = math.log(inlog, 10) 
     print(outlog) 

     # Here, the program will ask the user to quit or to continue 
     print("Want to check another one?") 
     response = input("Hit y for yes or n for no\n") 

     if response == ("y" or "Y"): 
      pass 
     elif response == ("n" or "N"): 
      break 
     else: 
      #I don't know what to do here so that the program asks the user to quit or continue if the response is invalid? 

    except ValueError: 
     print("Invalid Input: Make sure your number is greater than zero and no alphabets. Try Again.") 

else語句後,我希望程序要求用戶一次又一次地做出反應,直到它爲「Y」或「Y」和「N」或有效的反應「 N」。如果我在這裏添加另一個while循環,如果用戶輸入「y」,那麼對於pass語句會很有幫助。但是當用戶響應爲「n」時它不會中斷程序,因爲它會將我們置於外部循環中。 那麼如何整理呢?

+1

'響應==( 「Y」 或 「Y」)'不看的權利。你想在'Yy''上做出迴應。 – georg 2013-02-10 11:06:37

回答

1

你可以做這樣的事情:

stop=False 
while True: 
    try: 
     inlog = int(input("Enter any value greater than zero to lookup its logarithm to the base 10\n")) 
     outlog = math.log(inlog, 10) 
     print(outlog) 

     # Here, the program will ask the user to quit or to continue 
     print("Want to check another one?") 

     while True: 
      response = input("Hit y for yes or n for no\n") 
      if response == ("y" or "Y"): 
       stop = False 
       break 
      elif response == ("n" or "N"): 
       stop = True 
       break 
      else: 
       continue 
     if stop: 
      break 
except ValueError: 
     print("Invalid Input: Make sure your number 
+0

謝謝,雖然在第一行中不需要「stop = False」。謝謝。無論如何,這是最有效的解決方案。 – 2013-02-10 10:13:03

+0

@PreetikaSharma,是的,現在看起來就像那樣。但是我已經開始考慮其他事情了,所以它在那裏初始化了。 – Rohan 2013-02-10 10:14:53

+0

'else:繼續'在這裏沒有意義 – Eric 2013-02-10 13:38:16

0

您可以在初始值爲'false'的值之外定義一個布爾參數。在外循環的每次運行開始時,您可以檢查此布爾值,如果它爲真,那麼也會打破外循環。在此之後,當你想要在內部循環內部結束外部循環時,只需在打破內部循環之前使值爲真。這種方式外環也將打破。

4

您可以在測試移動到不同的功能:

def read_more(): 
    while True: 
     print("Want to check another one?") 
     response = input("Hit y for yes or n for no\n") 

     if response == ("y" or "Y"): 
      return True 
     elif response == ("n" or "N"): 
      return False 
     else: 
      continue 

然後在你的函數,只是測試這種方法的返回類型:

while True: 
    try: 
     inlog = int(input("Enter any value greater than zero to lookup its logarithm to the base 10\n")) 
     outlog = math.log(inlog, 10) 
     print(outlog) 

     if read_more(): 
      continue 
     else: 
      break 

需要注意的是,你可以進入無限循環,如果用戶繼續輸入錯誤的輸入。你可以限制他達到一些最大的嘗試。

+0

如果'in'運算符太高級,總會寫出布爾邏輯:'if response ==「y」or response ==「Y」' – lowerkey 2013-02-10 09:57:16

+1

等待,不是'(「y」或「Y 「)總是真的,所以如果response ==(」y「或」Y「):'實際上是'if response == True:',這總是False? – Volatility 2013-02-10 10:02:23

+0

@揮發性。啊!等待。你那裏有括號。我沒看見。對不起會更新答案。但是,儘管這可能會起作用,但是令人困惑,所以最好使用'in'運算符。 – 2013-02-10 10:07:59

0

試試這個:

import math 
class quitit(Exception): 
    pass 
print("Welcome to logarithm calculator") 

while True: 
    try: 
     inlog = int(input("Enter any value greater than zero to lookup its logarithm to the base 10\n")) 
     outlog = math.log(inlog, 10) 
     print(outlog) 

     # Here, the program will ask the user to quit or to continue 
     print("Want to check another one?") 
     while True: 
      response = input("Hit y for yes or n for no\n") 
      if response == ("y" or "Y"): 
       break 
      elif response == ("n" or "N"): 
       raise quitit 
except quitit: 
     print "Terminated!"   
except ValueError: 
     print("Invalid Input: Make sure your number is greater than zero and no alphabets. Try Again.") 
0

如果用戶輸入小於或等於零的數量,這將產生一個異常,你有沒有佔了。

只要突破循環,它不會嵌套,你不能已經爆發。如果你有更深的嵌套,那麼我會建議使用下面的模板來打破嵌套循環。

def loopbreak(): 
    while True: 
    while True: 
     print('Breaking out of function') 
     return 
    print('This statement will not print, the function has already returned') 
+0

loopbreak將簡單地從它自身返回。 – lowerkey 2013-02-10 10:09:30

+0

它意味着作爲一個模板。你甚至讀過'如果你有更深的嵌套,那麼我會提出以下建議。'有鑑於此,您的評論對我來說沒有意義。我不會爲這段代碼使用double while循環,但也想回答一般問題。 – Octipi 2013-02-10 10:15:59

+0

如果你在一個嵌套循環中,並想從中斷開,返回仍然會終止整個函數,而不僅僅是循環。這可能是你想要的,但它可能不是。公平地說,你確實說你正在破壞這個功能...... – lowerkey 2013-02-10 10:22:01

0
stop=False 
while not stop: 
    try: 
     inlog = int(input("Enter any value greater than zero to lookup its logarithm to the base 10\n")) 
     outlog = math.log(inlog, 10) 
     print(outlog) 

     # Here, the program will ask the user to quit or to continue 
     print("Want to check another one?") 

     while True: 
      response = raw_input("Hit y for yes or n for no\n") 
      if response == ("y" or "Y"): 
       break 
      elif response == ("n" or "N"): 
       stop = True 
       break 
    except ValueError: 
     print("Invalid Input: Make sure your number")