2013-02-10 92 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")