Python程序整数输入错误



我已经创建了一个python脚本来从用户获取手机号码

我需要将数字作为整数

所以我用了

Mobile=int(input("Enter the Mobile Number: "))

在运行程序时,如果输入任何字符而不是整数,则程序会崩溃并导致错误消息。

但是,我的需要是,它必须给出错误消息,并且必须再次运行以询问手机号码。

让我知道的方式.... !!

while True:
    given = input("Enter the Mobile Number: ")
    try:
        mobile = int(given)
        break
    except Exception:
        print("Invalid input, should only contain numbers")
print("entered number is", mobile)

最新更新