输入字段中添加的值不是int值时显示错误

时间:2018-11-18 14:45:36

标签: python python-3.x

我正在用python运行简单的程序。我正在使用输入法并使用以下代码

overflow = int(input("Enter the number"))
print(overflow)

我想如果有人在输入中输入“ sting”,那么输入将显示please enter number only

2 个答案:

答案 0 :(得分:1)

这是放置try-except blocklearn about exception handling的好地方。

overflow = input("Enter the number")
try:
    overflow = int(overflow)
    print(overflow)
except ValueError:
    print("Please enter number only")

try: overflow = int(input("Enter the number")) print(overflow) except ValueError: print("Please enter numbers only.") 块非常酷,在某种意义上,您是 force 一个错误,然后 catch (这就是try-except块的作用)。

except例外是...

  

当内置操作或函数接收到具有以下内容的参数时引发   正确的类型,但值不合适

您可以将其包装在ValueError块周围,以防止代码自动关闭或继续执行。

答案 1 :(得分:0)

希望这就是您想要的

Given: Search all shirts by url "http://shirts-shop.com/frankfurt"

Then (step 1): Search all cities where url is "http://shirts-shop.com/frankfurt" — it will return "Frankfurt" city

Then (step 2): Search all shirts by city "Frankfurt"