Python 使用循环的幂函数
我刚刚开始使用Python 2.7。 我正在尝试制作一个程序,它执行幂函数(使用循环),而不使用导入。数学 我想我明白了,只是我的程序不执行负指数。 输出结果仅为1。 这是我到目前为止所拥有的Python 使用循环的幂函数,python,python-2.7,function,loops,Python,Python 2.7,Function,Loops,我刚刚开始使用Python 2.7。 我正在尝试制作一个程序,它执行幂函数(使用循环),而不使用导入。数学 我想我明白了,只是我的程序不执行负指数。 输出结果仅为1。 这是我到目前为止所拥有的 decimal=float(input('Enter the base number:')) integer=int(input('Enter the exponent number:')) def power_function(decimal, integer): num=1 for
decimal=float(input('Enter the base number:'))
integer=int(input('Enter the exponent number:'))
def power_function(decimal, integer):
num=1
for function in range(integer):
if integer>0:
num=num*decimal
if integer<0:
num=1/(num*decimal)
return num
print power_function(decimal, integer)
decimal=float(输入('输入基数:'))
整数=int(输入('输入指数:'))
def power_函数(十进制、整数):
num=1
对于范围内的函数(整数):
如果整数>0:
num=num*decimal
如果整数根据负值范围固定
def power_function(decimal, integer):
num=1
if integer>0:
for function in range(integer):
num=num*decimal
if integer<0:
num=1.0 # force floating point division
for function in range(-integer):
num=num/decimal
return num
def power_函数(十进制、整数):
num=1
如果整数>0:
对于范围内的函数(整数):
num=num*decimal
如果整数根据负值范围固定
def power_function(decimal, integer):
num=1
if integer>0:
for function in range(integer):
num=num*decimal
if integer<0:
num=1.0 # force floating point division
for function in range(-integer):
num=num/decimal
return num
def power_函数(十进制、整数):
num=1
如果整数>0:
对于范围内的函数(整数):
num=num*decimal
如果integer简单的修复方法是为您的范围使用abs(integer)
:
def power_function(decimal, integer):
num = 1
for function in range(abs(integer)):
num = num*decimal if integer > 0 else num/decimal
return num
power_function(2, 2)
# 4
power_function(2, -2)
# 0.25
或者只是使用reduce:
def power_function(decimal, integer):
op = (lambda a, b: a*b) if integer > 0 else (lambda a, b: a/b)
return reduce(op, [decimal]*abs(integer), 1)
power_function(3, 3)
# 27
简单的解决方法是对您的范围使用abs(整数)
:
def power_function(decimal, integer):
num = 1
for function in range(abs(integer)):
num = num*decimal if integer > 0 else num/decimal
return num
power_function(2, 2)
# 4
power_function(2, -2)
# 0.25
或者只是使用reduce:
def power_function(decimal, integer):
op = (lambda a, b: a*b) if integer > 0 else (lambda a, b: a/b)
return reduce(op, [decimal]*abs(integer), 1)
power_function(3, 3)
# 27
我做了一些调整
def power_function (decimal, integer):
num = 1
for function in range (abs(integer)):
if integer > 0:
num *= decimal
if integer < 0:
num *= 1.0/decimal
if integer == 0:
num = 1
return num
def power_函数(十进制、整数):
num=1
对于范围内的函数(abs(整数)):
如果整数>0:
num*=十进制
如果整数<0:
num*=1.0/十进制
如果整数==0:
num=1
返回数
我做了一些调整
def power_function (decimal, integer):
num = 1
for function in range (abs(integer)):
if integer > 0:
num *= decimal
if integer < 0:
num *= 1.0/decimal
if integer == 0:
num = 1
return num
def power_函数(十进制、整数):
num=1
对于范围内的函数(abs(整数)):
如果整数>0:
num*=十进制
如果整数<0:
num*=1.0/十进制
如果整数==0:
num=1
返回数
range()
的负数是一个空列表,因此永远不会进入循环。啊!我明白了,那我该怎么办?是否需要使其在为负数时不进入循环?负数的range()
是一个空列表,因此从不进入循环。啊!我明白了,那我该怎么办?我需要让它在为负时不进入循环吗?我尝试了你的方法,但它仍然给我1的输出。对不起,当我重新编辑帖子时,我复制了你的num=1/(num*decimal)
表达式,而不是正确的num=num/decimal
表达式谢谢!它起作用了。不过我有个简单的问题。。为什么我需要把“num=1”作为一个全局术语?在2号线上?我仍然不能理解loopsIt不是全局的,但它确实需要在循环之外初始化,因为它在num=num*decimal
的右侧引用。如果未在循环外初始化,则会抛出一个错误(num
在定义之前使用)。我明白了。谢谢你,我真的很感激你,我尝试过你的方式,但它仍然给我一个1的输出。很抱歉,当我重新编辑这篇文章时,我用了你的num=1/(num*decimal)
表达式,而不是正确的num=num/decimal
表达式。非常感谢!它起作用了。不过我有个简单的问题。。为什么我需要把“num=1”作为一个全局术语?在2号线上?我仍然不能理解loopsIt不是全局的,但它确实需要在循环之外初始化,因为它在num=num*decimal
的右侧引用。如果未在循环外初始化,则会抛出一个错误(num
在定义之前使用)。我明白了。谢谢你,我真的很感激你,我只允许使用循环和条件句。我不允许使用任何类型的重要功能。你也可以使用匿名函数来实现它-不允许导入。如何实现?你介意给我看一下源代码吗?我绝对是一个初学者。。。即使你告诉我该怎么做,我也无法理解,因为我不知道很多东西。另外,为什么我需要将“num=1”作为全局变量?num=1
不是全局变量-它在函数中,只是它的初始值。我只允许使用循环和条件。我不允许使用任何类型的重要功能。你也可以使用匿名函数来实现它-不允许导入。如何实现?你介意给我看一下源代码吗?我绝对是一个初学者。。。即使你告诉我该怎么做,我也无法理解,因为我不知道很多东西。另外,为什么我需要将“num=1”作为全局值?num=1
不是全局值-它在函数中,只是它的初始值。