只要没有错误,如何运行while循环 [英] How to run a while loop as long as there is no error

查看:26
本文介绍了只要没有错误,如何运行while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个可能出错的代码,但我想运行它,只要没有错误.

I am running a code that might get an error, but I want to run it as long as there is no error.

我想过类似的事情:

while ValueError:
    try:
        x = int(input("Write a number: "))
    except ValueError:
        x = int(input("You must write a number: "))`

推荐答案

离你很近

while True:
    try:
        x = int(input("Write a number: "))
        break
    except ValueError:
        print("You must write a number: ")

要了解有关异常处理的更多信息,请参阅文档

To know more about exception handling, refer the documentation

这篇关于只要没有错误,如何运行while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆