Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.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 如何使用**运算符_Python_Operators - Fatal编程技术网

Python 如何使用**运算符

Python 如何使用**运算符,python,operators,Python,Operators,嗨,我正试图在Python中使用power操作符,但我不知道如何从用户输入组合。 任何人都可以帮助我如何使用它 import math input_1 = raw_input('Please enter an integer: ') input_2 = raw_input('Please enter another integer: ') var_result = input_1.int() ** input_2.int() print var_result 当我运行它时,会得到以下信息:

嗨,我正试图在Python中使用power操作符,但我不知道如何从用户输入组合。 任何人都可以帮助我如何使用它

import math
input_1 = raw_input('Please enter an integer:  ')
input_2 = raw_input('Please enter another integer:  ')
var_result = input_1.int() ** input_2.int()
print var_result
当我运行它时,会得到以下信息:

~ mgregory$ python foo.py
Please enter an integer:  12
Please enter another integer:  12
Traceback (most recent call last):
  File "foo.py", line 4, in <module>
    var_result = input_1.int() ** input_2.int()
AttributeError: 'str' object has no attribute 'int
您需要使用int类型来创建int对象

尝试:

也许你想要这个

import math
input_1 = raw_input('Please enter an integer:  ')
input_2 = raw_input('Please enter another integer:  ')
var_result = int(input_1) ** int(input_2)
print var_result
您需要将字符串input_1和input_转换为int

两个注释:

int使用不正确。它应该被称为intinput_1。您还可以在需要时键入int,请参见:

如果使用math.powinput_1、input_2,则参数将自动类型转换为float,这可能更方便详细信息:

所以你可以这样做:

import math
input_1 = raw_input('Please enter an integer:  ')
input_2 = raw_input('Please enter another integer:  ')
var_result = math.pow(input_1, input_2)
print var_result

你想做什么?你正在经历什么?我想,从长远来看,如果你完成了这篇文章,你会为自己省去很多麻烦。谢谢你,谢谢你的建议,我正在学习Python,我会尝试阅读它。谢谢你的解释。我现在明白了。
import math
input_1 = raw_input('Please enter an integer:  ')
input_2 = raw_input('Please enter another integer:  ')
var_result = int(input_1) ** int(input_2)
print var_result
import math
input_1 = raw_input('Please enter an integer:  ')
input_2 = raw_input('Please enter another integer:  ')
var_result = math.pow(input_1, input_2)
print var_result