How do I check if raw input is integer in python 2.7?

前端 未结 5 1313
庸人自扰
庸人自扰 2020-11-28 15:28

Is there a method that I can use to check if a raw_input is an integer?

I found this method after researching in the web:

print isinsta         


        
5条回答
  •  没有蜡笔的小新
    2020-11-28 16:19

    Try this method .isdigit(), see example below.

    user_input = raw_input()
    if user_input.isdigit():
        print "That is a number."
    
    else:
        print "That is not a number."
    

    If you require the input to remain digit for further use, you can add something like:

    new_variable = int(user_input)
    

提交回复
热议问题