Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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中显示的错误-TypeError:不支持**或pow()的操作数类型:';str';和';int';_Python_Python 2.7_Typeerror - Fatal编程技术网

Python中显示的错误-TypeError:不支持**或pow()的操作数类型:';str';和';int';

Python中显示的错误-TypeError:不支持**或pow()的操作数类型:';str';和';int';,python,python-2.7,typeerror,Python,Python 2.7,Typeerror,我今天刚开始学python。。在codeacademy上花了好几个小时,学到了很多东西,所以我想我应该自己制作一个程序来计算一个人和他们兄弟姐妹的年龄。非常基本,只是一个开始。我只是想看看我能做些什么。我一直收到标题中提到的这个打字错误。这是我的密码: a=raw_input(''enter age'') b=raw_input(''sibling's age'') Square=a**2 + b**2 + 2ab If square <160: Print 'really y

我今天刚开始学python。。在codeacademy上花了好几个小时,学到了很多东西,所以我想我应该自己制作一个程序来计算一个人和他们兄弟姐妹的年龄。非常基本,只是一个开始。我只是想看看我能做些什么。我一直收到标题中提到的这个打字错误。这是我的密码:

a=raw_input(''enter age'') 
b=raw_input(''sibling's age'') 
Square=a**2 + b**2 + 2ab
If square <160:
    Print 'really young people' 
Else:
    Print 'square of sum >160'
a=原始输入(“输入年龄”)
b=原始输入(“兄弟姐妹年龄”)
正方形=a**2+b**2+2ab

如果square首先也是最重要的,如果您刚刚开始,那么您确实应该学习Python 3,而不是Python 2

你发布的代码有很多问题。下面是一行一行的细分,并在Python 3中进行了更正:

a=raw_input(''enter age'') 
b=raw_input(''sibling's age'')
您需要使用单引号
或双引号
,但不能使用两个单引号来定义字符串。请尝试:

a = int(input("Enter age"))
b = int(input("Sibling's age"))
Python 3中没有
raw_input()
,它只是。此函数还返回一个字符串,因此如果要对其进行数学运算,需要调用以将结果转换为整数

Square=a**2 + b**2 + 2ab
只要
a
b
是int,表达式的前两部分就可以了,但您需要记住Python中的所有操作都是显式的,因此您需要像这样修复最后一部分:

Square = a**2 + b**2 + 2*a*b
现在是最后一部分:

If square <160:
    Print 'really young people' 
Else:
    Print 'square of sum >160'

下面是完整的Python 3代码:

a = int(input("Enter age"))
b = int(input("Sibling's age"))
Square = a**2 + b**2 + 2*a*b
if Square < 160:
    print("Really young people")
else:
    print("square of sum > 160")
a=int(输入(“输入年龄”))
b=int(输入(“兄弟姐妹年龄”))
正方形=a**2+b**2+2*a*b
如果平方<160:
印刷品(“真正的年轻人”)
其他:
打印(“总和的平方>160”)

从学习Python 3开始,而不是2。即使在出现类型问题之前,这段代码也充满了语法错误。你需要发布你实际运行的代码。好吧,但这在Python 3中可以工作吗?@user2357112这是我运行的代码……在数学问题解决之前,它工作得很好part@KarlStark:不,不是。如果您尝试运行此代码,您将得到一个语法Error而不是TypeError。这段代码与您实际在每一行上运行的代码不同,因为每一行都有语法问题,在TypeError出现之前,可能会中止使用SyntaxError进行编译。虽然学习Python 3是一个很好的举措,但我认为将语法更正和Python 2->3转换混合在一起会造成混乱也许是这样。@user2357112,但由于这是OP学习Python的第一天,我想让他们尽快走上正轨。
a = int(input("Enter age"))
b = int(input("Sibling's age"))
Square = a**2 + b**2 + 2*a*b
if Square < 160:
    print("Really young people")
else:
    print("square of sum > 160")