Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 回溯=Can';t转换为';类型';对象隐式地访问str_Python - Fatal编程技术网

Python 回溯=Can';t转换为';类型';对象隐式地访问str

Python 回溯=Can';t转换为';类型';对象隐式地访问str,python,Python,问题: 回溯(最近一次呼叫最后一次): 文件“C:/Python34/my code/types.py”,第2行,打印( 种族,'is'+类型(种族))类型错误:无法将'type'对象转换为 隐式str 打开一个空闲编辑窗口,通过为变量分配字符串来初始化变量,然后显示其值和数据类型 race = ‘Daytona 500’ print( race , ‘is‘ + type( race ) ) kilo = 1000 print( kilo , ‘is‘ + type( kilo ) ) t

问题

回溯(最近一次呼叫最后一次):

文件“C:/Python34/my code/types.py”,第2行,打印( 种族,'is'+类型(种族))类型错误:无法将'type'对象转换为 隐式str

打开一个空闲编辑窗口,通过为变量分配字符串来初始化变量,然后显示其值和数据类型

race = ‘Daytona 500’
print( race , ‘is‘ + type( race ) )
kilo = 1000
print( kilo , ‘is‘ + type( kilo ) )
temp = 98.6
print( temp , ‘is‘ + type( temp ) )
flag = True
print( flag , ‘is‘ + type( flag ) )
flag = 4 > 8
print( flag , ‘is‘ + type( flag ) )
接下来,通过给变量赋值来初始化变量,然后显示其值和数据类型

race = ‘Daytona 500’
print( race , ‘is‘ + type( race ) )
kilo = 1000
print( kilo , ‘is‘ + type( kilo ) )
temp = 98.6
print( temp , ‘is‘ + type( temp ) )
flag = True
print( flag , ‘is‘ + type( flag ) )
flag = 4 > 8
print( flag , ‘is‘ + type( flag ) )
现在,通过指定一个十进制数来初始化变量,然后显示其值和数据类型

race = ‘Daytona 500’
print( race , ‘is‘ + type( race ) )
kilo = 1000
print( kilo , ‘is‘ + type( kilo ) )
temp = 98.6
print( temp , ‘is‘ + type( temp ) )
flag = True
print( flag , ‘is‘ + type( flag ) )
flag = 4 > 8
print( flag , ‘is‘ + type( flag ) )
通过指定一个真值关键字初始化变量,然后显示其值和数据类型

race = ‘Daytona 500’
print( race , ‘is‘ + type( race ) )
kilo = 1000
print( kilo , ‘is‘ + type( kilo ) )
temp = 98.6
print( temp , ‘is‘ + type( temp ) )
flag = True
print( flag , ‘is‘ + type( flag ) )
flag = 4 > 8
print( flag , ‘is‘ + type( flag ) )
最后,用比较的真值结果替换最后一个变量值,然后再次显示其值和数据类型

race = ‘Daytona 500’
print( race , ‘is‘ + type( race ) )
kilo = 1000
print( kilo , ‘is‘ + type( kilo ) )
temp = 98.6
print( temp , ‘is‘ + type( temp ) )
flag = True
print( flag , ‘is‘ + type( flag ) )
flag = 4 > 8
print( flag , ‘is‘ + type( flag ) )
保存,然后运行程序以发现已创建变量中存储的数据类型

好的,那么这就是我编码的错误吗?:

race = 'Daytona 500'
print( race , 'is' + type( race ))

kilo = 1000
print( kilo , 'is' + type( kilo))

temp = 98.6
print( temp , 'is' + type( temp))

flag = True
print( flag , 'is' + type( flag))

flag = 4>8
print( flag , 'is' + type( flag))
试试这个:

   race = 'Daytona 500'

   print( race , 'is' + str(type( race )))

   kilo = 1000

   print( kilo , 'is' + str(type( kilo)))

   temp = 98.6

   print( temp , 'is' + str(type( temp)))

   flag = True

   print( flag , 'is' + str(type( flag)))

   flag = 4>8

   print( flag , 'is' + str(type( flag)))
如果试图将非字符串类型连接到字符串,Python希望显式类型转换为
str

尝试以下操作:

   race = 'Daytona 500'

   print( race , 'is' + str(type( race )))

   kilo = 1000

   print( kilo , 'is' + str(type( kilo)))

   temp = 98.6

   print( temp , 'is' + str(type( temp)))

   flag = True

   print( flag , 'is' + str(type( flag)))

   flag = 4>8

   print( flag , 'is' + str(type( flag)))

如果试图将非字符串类型连接到字符串,Python希望显式类型转换为
str

您不需要使用+符号,而是用逗号替换。那么代码就是:

race = 'Daytona 500'

print( race , 'is' , type( race ))

kilo = 1000

print( kilo , 'is' , type( kilo))

temp = 98.6

print( temp , 'is' , type( temp))

flag = True

print( flag , 'is' , type( flag))

flag = 4>8

print( flag , 'is' , type( flag))

您不需要使用+符号,而是用逗号替换。那么代码就是:

race = 'Daytona 500'

print( race , 'is' , type( race ))

kilo = 1000

print( kilo , 'is' , type( kilo))

temp = 98.6

print( temp , 'is' , type( temp))

flag = True

print( flag , 'is' , type( flag))

flag = 4>8

print( flag , 'is' , type( flag))

请使用编辑窗口中的
{}
按钮正确格式化代码。请使用编辑窗口中的
{}
按钮正确格式化代码。为什么不需要str(race)?
race
已经是
str
类型(您将其定义为字符串),而其他类型则是
类型,
int
double
bool
您不需要在
print(str(kilo),'is'+str(type(kilo))
中“str-ize”
kilo
即可。问题在于
'is'+type(kilo)
中尝试的字符串串联。当你这样做的时候,
'is'+str(type(kilo))
你得到了
“isint”
(注意没有空格)。为什么不需要str(race)?
race
已经是一种
str
(你把它定义为一个字符串),而其他类型则是
type
int
double
bool
,你不需要“str-ize”
kilo
print(str(kilo),'is'+str(type(kilo))
print
将为您实现这一点。这就是问题所在。
'is'+type(kilo)
'is'+str(type(kilo))
中尝试串接时,您会得到
“isint”
(注意缺少空格)。