如何使此循环继续前进,直到输入正确的输入?

发布于 2025-02-12 05:15:02 字数 470 浏览 1 评论 0原文

这是我编写的代码,我现在正在学习python,但是告诉我如何使这个循环继续前进,直到我们达到x和y的正确条件是整数和y> 0,这样它就不会抛出异常。

def problem_2():
    while True:
        try:
            x = int(input("Enter the value of X"))
            y = int(input("Enter the value of Y"))
            print(x/y)
        except ZeroDivisionError:
            print("You have divided the number by zero")
        except TypeError:
            print("Please Enter Integer in both the cases")

problem_2()

This is the code that I have written, I am learning python right now but tell me how can I make this loop keep on going until we hit the correct condition that is x and y are both integers and y > 0, so that it won't be able to throw an exception.

def problem_2():
    while True:
        try:
            x = int(input("Enter the value of X"))
            y = int(input("Enter the value of Y"))
            print(x/y)
        except ZeroDivisionError:
            print("You have divided the number by zero")
        except TypeError:
            print("Please Enter Integer in both the cases")

problem_2()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海 2025-02-19 05:15:02

您可以简单地做一个无限的时,循环以X和Y为输入并检查您的状况。

  1. 如果满足您的病情,请进行您想做的处理并打破循环。
  2. 如果您没有满足条件,请再次继续循环,同一件事将重复直到满足条件。
while true:
    #take your input
    #check your condition 
    #if condition not met 
        continue
    #if condition met
        #do your stuff
        break

You can simply make a infinite while loop take x and y as input and check you condition.

  1. If your condition are met then do the processing which you want to do and break the loop.
  2. If you condition are not met then just continue the loop again and same thing will repeat until you conditions are met.
while true:
    #take your input
    #check your condition 
    #if condition not met 
        continue
    #if condition met
        #do your stuff
        break
蒲公英的约定 2025-02-19 05:15:02

检查y> 0在您的例外捕获块之后:

def problem_2():
    while True:
        try:
            x = int(input("Enter the value of X"))
            y = int(input("Enter the value of Y"))
            print(x/y)
        except ZeroDivisionError:
            print("You have divided the number by zero")
        except TypeError:
            print("Please Enter Integer in both the cases")
        # No exception was raised, check y
        if y > 0:
            break

problem_2()

另外,为了避免使用异常处理,您可以这样做:

def problem_2():
    while True:
        x = input("Enter the value of X")
        y = input("Enter the value of Y")
        if not x.isdigit() or not y.isdigit():
            print("Please enter Integer in both the cases")
            continue
        x, y = int(x), int(y)
        if x == 0 or y == 0:
            print("Cannot divide by zero")
            continue
        # If we reached here, the input is valid
        print(x/y)
        break

problem_2()

Check if y > 0 after your exception catching blocks:

def problem_2():
    while True:
        try:
            x = int(input("Enter the value of X"))
            y = int(input("Enter the value of Y"))
            print(x/y)
        except ZeroDivisionError:
            print("You have divided the number by zero")
        except TypeError:
            print("Please Enter Integer in both the cases")
        # No exception was raised, check y
        if y > 0:
            break

problem_2()

Alternatively, to avoid using exception handling altogether, you can do it like this:

def problem_2():
    while True:
        x = input("Enter the value of X")
        y = input("Enter the value of Y")
        if not x.isdigit() or not y.isdigit():
            print("Please enter Integer in both the cases")
            continue
        x, y = int(x), int(y)
        if x == 0 or y == 0:
            print("Cannot divide by zero")
            continue
        # If we reached here, the input is valid
        print(x/y)
        break

problem_2()
仅此而已 2025-02-19 05:15:02

尝试这个我希望它能适合您想要的东西。谢谢

def problem_2():
    while True:
        x = input("Enter the value of x: ")
        y = input("Enter the value of y: ")
        if x.isdigit() and y.isdigit():
            if int(y)>0:
                print(int(x)/int(y))
            else:
                pass 
        else:
            continue
problem_2()

Try this one i hope it will work perfect for what you are wanting.Thanks

def problem_2():
    while True:
        x = input("Enter the value of x: ")
        y = input("Enter the value of y: ")
        if x.isdigit() and y.isdigit():
            if int(y)>0:
                print(int(x)/int(y))
            else:
                pass 
        else:
            continue
problem_2()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文