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”时它不会破坏程序,因为它会使我们陷入外循环。那么如何解决这个问题呢?

4

6 回答 6

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

请注意,如果用户继续输入错误的输入,您可以进入无限循环。您可以限制他最多尝试一些最大尝试。

于 2013-02-10T09:49:02.060 回答
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
于 2013-02-10T09:49:50.180 回答
0

您可以在初始值为“false”的值之外定义一个布尔参数。在外循环的每次运行开始时,您可以检查此布尔值,如果为真,则也中断外循环。之后,当你想在内循环内结束外循环时,只需在打破内循环之前将值设为真。这样外循环也会中断。

于 2013-02-10T09:41:42.460 回答
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.")
于 2013-02-10T09:51:40.927 回答
0

如果用户输入一个小于或等于零的数字,这将产生一个您没有考虑到的异常。

就跳出循环而言,它不是太嵌套以至于您不能已经跳出。如果您有更深的嵌套,那么我建议您使用以下模板来打破嵌套循环。

def loopbreak():
  while True:
    while True:
      print('Breaking out of function')
      return
  print('This statement will not print, the function has already returned')
于 2013-02-10T10:03:04.407 回答
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")
于 2013-02-10T11:29:41.770 回答