Python程序整数inpu

2024-04-19 00:30:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我需要得到整数

所以我用

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

在运行我的程序时,如果输入任何字符而不是整数,程序将崩溃并产生错误消息

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

请让我知道路


Tags: the用户程序脚本消息numberinput错误
1条回答
网友
1楼 · 发布于 2024-04-19 00:30:29
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)

相关问题 更多 >