如何在python中创建一个使用嵌套while循环执行指数运算的程序?
我需要输出为:如何在python中创建一个使用嵌套while循环执行指数运算的程序?,python,Python,我需要输出为: 2 to the power 3 = 8 3 to the power 3 = 8 4 to the power 3 = 8 5 to the power 3 = 8 我需要使用嵌套的while循环,不能使用求幂运算符(**)。我将感谢任何帮助,并提前感谢您 在进入嵌套循环之前,需要重置电源和j 2 to the power 3 = 8 3 to the power 3 = 27 4 to the power 3 = 64 5 to the power 3 = 12
2 to the power 3 = 8
3 to the power 3 = 8
4 to the power 3 = 8
5 to the power 3 = 8
我需要使用嵌套的while循环,不能使用求幂运算符(**)。我将感谢任何帮助,并提前感谢您 在进入嵌套循环之前,需要重置
电源
和j
2 to the power 3 = 8
3 to the power 3 = 27
4 to the power 3 = 64
5 to the power 3 = 125
x=eval(输入('输入一个大于1的数字:'))
e=eval(输入('输入大于1的指数集:'))
i=2
当i进入嵌套循环之前,需要重置电源
和j
2 to the power 3 = 8
3 to the power 3 = 27
4 to the power 3 = 64
5 to the power 3 = 125
x=eval(输入('输入一个大于1的数字:'))
e=eval(输入('输入大于1的指数集:'))
i=2
而i这只能通过加法完成,对于x**power
:
x=eval(input('Enter a number greater than 1: '))
e=eval(input('Enter an exponenet greater than 1: '))
i=2
while i <= x:
j = 1
power = 1
while j <=e:
power=power*i
j=j+1
print(i, 'to the power',e, '=', power)
i=i+1
但是请注意,如果power=0
失败,就像power=-1
失败一样,这只能通过添加来完成,对于x**power
:
x=eval(input('Enter a number greater than 1: '))
e=eval(input('Enter an exponenet greater than 1: '))
i=2
while i <= x:
j = 1
power = 1
while j <=e:
power=power*i
j=j+1
print(i, 'to the power',e, '=', power)
i=i+1
但是请注意,如果power=0
失败,就像power=-1
一样,更“低技术”的方法依次定义了后续函数、加法、乘法和幂运算
def成功(x):
返回x
def添加(x,y):
结果=x
当y>0时:
结果=成功(x)
y-=1
返回结果
def mult(x,y):
结果=x
当y>1时:
结果=添加(结果,x)
y-=1
返回结果
def exp(x,y):
结果=1
当y>0时:
结果=mult(结果,x)
y-=1
返回结果
这四个都是关于一元数的典型例子。它们最好使用递归或for
循环来实现
有关下一个(超)操作,请参见。更“低技术”的操作方法依次定义函数的后续、加法、乘法和求幂
def成功(x):
返回x
def添加(x,y):
结果=x
当y>0时:
结果=成功(x)
y-=1
返回结果
def mult(x,y):
结果=x
当y>1时:
结果=添加(结果,x)
y-=1
返回结果
def exp(x,y):
结果=1
当y>0时:
结果=mult(结果,x)
y-=1
返回结果
这四个都是关于一元数的典型例子。它们最好使用递归或for
循环来实现
有关下一个(超)操作,请参阅。给你
代码中唯一的问题是变量没有重新初始化为1。
我帮你修好了
value = x #ex 3**3
temp = x
result = x
while power > 1:
while value > 1:
temp += result #1st loop: 3 + 3 + 3 = 9
value -= 1 #2nd loop 9 + 9 + 9 = 27
result = temp
value = x
power -= 1
print(f"{x} to the power of {power} is {result}")
x=eval(输入('输入一个大于1的数字:'))
e=eval(输入('输入大于1的指数集:'))
i=2
j=1
功率=1
而我给你
代码中唯一的问题是变量没有重新初始化为1。
我帮你修好了
value = x #ex 3**3
temp = x
result = x
while power > 1:
while value > 1:
temp += result #1st loop: 3 + 3 + 3 = 9
value -= 1 #2nd loop 9 + 9 + 9 = 27
result = temp
value = x
power -= 1
print(f"{x} to the power of {power} is {result}")
x=eval(输入('输入一个大于1的数字:'))
e=eval(输入('输入大于1的指数集:'))
i=2
j=1
功率=1
我非常感谢你!非常感谢你!对我来说,这似乎是一个作业问题。你应该试着自己解决这些问题。对我来说,这似乎是一个作业问题。你应该试着自己解决这些问题。