Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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 uTypeError:-:';的操作数类型不受支持;str';和';str';_Python - Fatal编程技术网

Python uTypeError:-:';的操作数类型不受支持;str';和';str';

Python uTypeError:-:';的操作数类型不受支持;str';和';str';,python,Python,我得到这个错误: uTypeError:-:“str”和“str”的操作数类型不受支持 根据该代码: print "what is your name?" x=raw_input() print "Are you woman or man?" y=raw_input() print "how old are you?" z=raw_input() print "at what age did you or will you first travel in an plane?" f=raw_i

我得到这个错误:

uTypeError:-:“str”和“str”的操作数类型不受支持

根据该代码:

print "what is your name?"  
x=raw_input()
print "Are you woman or man?"
y=raw_input()
print "how old are you?"
z=raw_input()
print "at what age did you or will you first travel in an plane?"
f=raw_input()

print "This is a story about a ",y," named ",x
print z-f ,"years ago",x, " first took an airplane."
print " the end"

为什么?

您的变量
z
f
都是字符串。Python不支持从另一个字符串中减去一个字符串

如果要显示数字,必须将其转换为浮点数或整数:

print int(z) - int(f),"years ago",x, " first took an airplane."

这些字符串之所以是字符串,首先是因为
raw\u input

变量
z
f
是字符串。Python不支持从另一个字符串中减去一个字符串

如果要显示数字,必须将其转换为浮点数或整数:

print int(z) - int(f),"years ago",x, " first took an airplane."
这些字符串之所以是字符串,首先是因为
raw\u input

返回一个字符串,因此基本上以以下内容结束:

print '20'-'11'
因为不能从一个字符串中减去另一个字符串,所以需要先将它们转换为数字

试试这个:

z = float(raw_input())
...
f = float(raw_input())
第二,使用描述性变量名,而不是x、y、z和f,例如:

age = float(raw_input())
...
firstPlaneTravelAge = float(raw_input())

print age-firstPlaneTravelAge, ...
还请注意,此代码没有验证,如果用户输入的不是数字的内容,它将崩溃。

返回一个字符串,因此基本上您会得到以下结果:

print '20'-'11'
因为不能从一个字符串中减去另一个字符串,所以需要先将它们转换为数字

试试这个:

z = float(raw_input())
...
f = float(raw_input())
第二,使用描述性变量名,而不是x、y、z和f,例如:

age = float(raw_input())
...
firstPlaneTravelAge = float(raw_input())

print age-firstPlaneTravelAge, ...

还请注意,此代码没有验证,如果用户输入的不是数字的内容,它将崩溃。

非常感谢,非常有帮助:)非常感谢,非常有帮助:)回滚,以便问题与答案匹配@用户1240834,如果您有一个新问题,请打开一个新问题,而不是编辑此问题。回滚以使问题与答案匹配@用户1240834,如果您有新问题,请打开新问题,而不是编辑此问题。