如何使此循环继续前进,直到输入正确的输入?
这是我编写的代码,我现在正在学习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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以简单地做一个无限的时,循环以X和Y为输入并检查您的状况。
You can simply make a infinite while loop take x and y as input and check you condition.
检查y> 0在您的例外捕获块之后:
另外,为了避免使用异常处理,您可以这样做:
Check if y > 0 after your exception catching blocks:
Alternatively, to avoid using exception handling altogether, you can do it like this:
尝试这个我希望它能适合您想要的东西。谢谢
Try this one i hope it will work perfect for what you are wanting.Thanks