Python 当用户留下错误的格式输入时,如何通知错误?

Python 当用户留下错误的格式输入时,如何通知错误?,python,if-statement,integer,Python,If Statement,Integer,当用户按要求插入非整数的输入时,如何通知错误 例如: number = int(input("Input the number: ")) 我想编写这样的代码:如果数字“不是”整数,那么 if number is not integers print("The input was not numerical value. Please try again") else: print (&qu

当用户按要求插入非整数的输入时,如何通知错误

例如:

number = int(input("Input the number: "))
我想编写这样的代码:如果数字“不是”整数,那么

if number is not integers 
            print("The input was not numerical value. Please try again")
        else:
            print ("Input "+ str(number) + " elements in the list:")
你能帮我吗


谢谢大家!

try/except语句应该非常适合:

try:
    number = int(input("Input the number: "))
    print ("Input "+ str(number) + " elements in the list:")
except ValueError:
    print("The input was not numerical value. Please try again")

你能举一个例子说明
int
可以产生的非整数吗?关于这一点已经很少有帖子了,例如:@ScottHunter你不明白我的意思。例如:有人为数字输入了非整数字符,因此系统将打印“输入的不是数值。请重试”。您没有得到我的意思:
number
在您进行任何测试之前,已经分配了调用
int
的结果。