Python 带整数的type()函数?

Python 带整数的type()函数?,python,python-2.7,Python,Python 2.7,我一直在编写一个代码,其中一部分给我带来了很多麻烦。就是这样 import math number=raw_input("Enter the number you wish to find its square root: ") word=number if type(word)==type(int): print sqrt(word) 在IDLE中,每当我键入一个数字时,都不会打印任何内容。我在编辑器中检查了语法错误和缩进,并修复了所有错误。您正在查找isinstance(): 但这

我一直在编写一个代码,其中一部分给我带来了很多麻烦。就是这样

import math
number=raw_input("Enter the number you wish to find its square root: ")
word=number
if type(word)==type(int):
    print sqrt(word)

在IDLE中,每当我键入一个数字时,都不会打印任何内容。我在编辑器中检查了语法错误和缩进,并修复了所有错误。

您正在查找
isinstance()

但这不起作用,因为
raw\u input()
返回一个字符串。您可能需要进行异常处理:

try:
    word = int(word)
except ValueError:
    print 'not a number!'
else:
    print sqrt(word)
对于您的特定错误,
type(word)is int
可能也会起作用,但这并不是很像python
type(int)
返回
int
类型的类型,即

>类型(42)
>>>类型(42)是int
真的
>>>类型(int)
>>>类型(int)是类型
真的

原始输入返回一个字符串。 您需要将输入转换为数字

in_string = raw_input("...")
try:
    number = float(in_string)
    print math.sqrt(number)
except ValueError as e:
    print "Sorry, {} is not a number".format(in_string)

想一想:你期望做什么?
>>> type(42)
<type 'int'>
>>> type(42) is int
True
>>> type(int)
<type 'type'>
>>> type(int) is type
True
in_string = raw_input("...")
try:
    number = float(in_string)
    print math.sqrt(number)
except ValueError as e:
    print "Sorry, {} is not a number".format(in_string)